currency.c
author rubidium
Thu, 17 Aug 2006 20:22:35 +0000
changeset 4297 d0311b62255c
parent 4293 4b7006c1b5eb
child 4311 2ac86c9c0ced
permissions -rw-r--r--
(svn r5934) -Cleanup: forgot some conversions to Year and to Date
-Cleanup: use _cur_year instead of _date for some (year based) comparisons
-Cleanup: remove a magic number in favour of another (less) magic number
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     1
/* $Id$ */
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     2
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     3
#include "stdafx.h"
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     4
#include "openttd.h"
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     5
#include "currency.h"
2306
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
     6
#include "news.h"
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     7
#include "variables.h"
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
     8
#include "table/strings.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4138
diff changeset
     9
#include "date.h"
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    10
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    11
// exchange rate    prefix
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    12
// |  separator        |     postfix
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    13
// |   |    Euro year  |       |
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    14
// |   |    |          |       |
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    15
CurrencySpec _currency_specs[] = {
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    16
	{    1, ',', CF_NOEURO, "\xA3", ""     }, // british pounds
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    17
	{    2, ',', CF_NOEURO, "$",    ""     }, // us dollars
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    18
	{    2, ',', CF_ISEURO, "¤",    ""     }, // Euro
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    19
	{  200, ',', CF_NOEURO, "\xA5", ""     }, // yen
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    20
	{   19, ',', 2002,      "",     " S."  }, // austrian schilling
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    21
	{   57, ',', 2002,      "BEF ", ""     }, // belgian franc
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    22
	{    2, ',', CF_NOEURO, "CHF ", ""     }, // swiss franc
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    23
	{   50, ',', CF_NOEURO, "",     " Kc"  }, // czech koruna // TODO: Should use the "c" with an upside down "^"
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    24
	{    4, '.', 2002,      "DM ",  ""     }, // deutsche mark
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    25
	{   10, '.', CF_NOEURO, "",     " kr"  }, // danish krone
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    26
	{  200, '.', 2002,      "Pts ", ""     }, // spanish pesetas
2495
b9864001ab31 (svn r3021) -Fix: "The finnish markka was never abbreviated with capital letters." (ln-)
tron
parents: 2468
diff changeset
    27
	{    8, ',', 2002,      "",     " mk"  }, // finnish markka
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    28
	{   10, '.', 2002,      "FF ",  ""     }, // french francs
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    29
	{  480, ',', 2002,      "",     "Dr."  }, // greek drachma
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    30
	{  376, ',', 2002,      "",     " Ft"  }, // hungarian forint
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    31
	{  130, '.', CF_NOEURO, "",     " Kr"  }, // icelandic krona
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    32
	{ 2730, ',', 2002,      "",     " L."  }, // italian lira
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    33
	{    3, ',', 2002,      "NLG ", ""     }, // dutch gulden
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    34
	{   11, '.', CF_NOEURO, "",     " Kr"  }, // norwegian krone
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    35
	{    6, ' ', CF_NOEURO, "",     " zl"  }, // polish zloty
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    36
	{    6, '.', CF_NOEURO, "",     " Lei" }, // romanian Lei
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    37
	{    5, ' ', CF_NOEURO, "",     " p"   }, // russian rouble
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    38
	{   13, '.', CF_NOEURO, "",     " Kr"  }, // swedish krona
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    39
	{    1, ' ', CF_NOEURO, "",     ""     }, // custom currency
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    40
};
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    41
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    42
const StringID _currency_string_list[] = {
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    43
	STR_CURR_GBP,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    44
	STR_CURR_USD,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    45
	STR_CURR_EUR,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    46
	STR_CURR_YEN,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    47
	STR_CURR_ATS,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    48
	STR_CURR_BEF,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    49
	STR_CURR_CHF,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    50
	STR_CURR_CZK,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    51
	STR_CURR_DEM,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    52
	STR_CURR_DKK,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    53
	STR_CURR_ESP,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    54
	STR_CURR_FIM,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    55
	STR_CURR_FRF,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    56
	STR_CURR_GRD,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    57
	STR_CURR_HUF,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    58
	STR_CURR_ISK,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    59
	STR_CURR_ITL,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    60
	STR_CURR_NLG,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    61
	STR_CURR_NOK,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    62
	STR_CURR_PLN,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    63
	STR_CURR_ROL,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    64
	STR_CURR_RUR,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    65
	STR_CURR_SEK,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    66
	STR_CURR_CUSTOM,
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    67
	INVALID_STRING_ID
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    68
};
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    69
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    70
// NOTE: Make sure both lists are in the same order
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    71
// + 1 string list terminator
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    72
assert_compile(lengthof(_currency_specs) + 1 == lengthof(_currency_string_list));
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    73
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    74
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    75
// get a mask of the allowed currencies depending on the year
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    76
uint GetMaskOfAllowedCurrencies(void)
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    77
{
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    78
	uint mask = 0;
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    79
	uint i;
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    80
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    81
	for (i = 0; i != lengthof(_currency_specs); i++) {
4297
d0311b62255c (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
    82
		Year to_euro = _currency_specs[i].to_euro;
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    83
4293
4b7006c1b5eb (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4286
diff changeset
    84
		if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
4b7006c1b5eb (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4286
diff changeset
    85
		if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    86
		mask |= (1 << i);
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    87
	}
3596
6654541a3958 (svn r4487) - Codechange: replace the custom currency magic number 23 with a define
peter1138
parents: 2495
diff changeset
    88
	mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency
2291
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    89
	return mask;
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    90
}
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    91
c142846954ee (svn r2815) Store the currency information in one central place instead of scattering it in several unrelated files
tron
parents:
diff changeset
    92
2306
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    93
void CheckSwitchToEuro(void)
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    94
{
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    95
	if (_currency_specs[_opt.currency].to_euro != CF_NOEURO &&
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    96
			_currency_specs[_opt.currency].to_euro != CF_ISEURO &&
4293
4b7006c1b5eb (svn r5926) -Codechange: make _cur_year contain the full year, instead of the offset since 1920
rubidium
parents: 4286
diff changeset
    97
			_cur_year >= _currency_specs[_opt.currency].to_euro) {
2306
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    98
		_opt.currency = 2; // this is the index of euro above.
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
    99
		AddNewsItem(STR_EURO_INTRODUCE, NEWS_FLAGS(NM_NORMAL, 0, NT_ECONOMY, 0), 0, 0);
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
   100
	}
adb71c055afb (svn r2830) Move CheckSwitchToEuro() to currency.[ch] and hide the truth about the custom currency behind a #define
tron
parents: 2291
diff changeset
   101
}
4138
8640f25e2510 (svn r5496) -CodeChange: Removed two compiler warnings
belugas
parents: 3596
diff changeset
   102