tron@2186: /* $Id$ */ tron@2186: belugas@6449: /** @file currency.cpp **/ belugas@6449: tron@2291: #include "stdafx.h" tron@2291: #include "openttd.h" tron@2291: #include "currency.h" rubidium@9259: #include "news_func.h" rubidium@8766: #include "settings_type.h" rubidium@8760: #include "date_func.h" rubidium@8760: tron@2291: #include "table/strings.h" tron@2291: belugas@4377: // exchange rate prefix symbol_pos belugas@4377: // | separator | postfix | belugas@4377: // | | Euro year | | | name belugas@4377: // | | | | | | | tron@5023: static const CurrencySpec origin_currency_specs[NUM_CURRENCY] = { belugas@6449: { 1, ',', CF_NOEURO, "£", "", 0, STR_CURR_GBP }, ///< british pounds belugas@6449: { 2, ',', CF_NOEURO, "$", "", 0, STR_CURR_USD }, ///< us dollars belugas@6449: { 2, ',', CF_ISEURO, "€", "", 0, STR_CURR_EUR }, ///< Euro belugas@6449: { 220, ',', CF_NOEURO, "¥", "", 0, STR_CURR_YEN }, ///< yen belugas@6449: { 20, ',', 2002, "", " S.", 1, STR_CURR_ATS }, ///< austrian schilling belugas@6449: { 59, ',', 2002, "BEF ", "", 0, STR_CURR_BEF }, ///< belgian franc belugas@6449: { 2, ',', CF_NOEURO, "CHF ", "", 0, STR_CURR_CHF }, ///< swiss franc belugas@6449: { 41, ',', CF_NOEURO, "", " Kč", 1, STR_CURR_CZK }, ///< czech koruna belugas@6449: { 3, '.', 2002, "DM ", "", 0, STR_CURR_DEM }, ///< deutsche mark belugas@6449: { 11, '.', CF_NOEURO, "", " kr", 1, STR_CURR_DKK }, ///< danish krone belugas@6449: { 245, '.', 2002, "Pts ", "", 0, STR_CURR_ESP }, ///< spanish pesetas belugas@6449: { 9, ',', 2002, "", " mk", 1, STR_CURR_FIM }, ///< finnish markka belugas@6449: { 10, '.', 2002, "FF ", "", 0, STR_CURR_FRF }, ///< french francs belugas@6449: { 500, ',', 2002, "", "Dr.", 1, STR_CURR_GRD }, ///< greek drachma belugas@6449: { 378, ',', 2010, "", " Ft", 1, STR_CURR_HUF }, ///< hungarian forint belugas@6449: { 130, '.', CF_NOEURO, "", " Kr", 1, STR_CURR_ISK }, ///< icelandic krona belugas@6449: { 2850, ',', 2002, "", " L.", 1, STR_CURR_ITL }, ///< italian lira belugas@6449: { 3, ',', 2002, "NLG ", "", 0, STR_CURR_NLG }, ///< dutch gulden belugas@6449: { 12, '.', CF_NOEURO, "", " Kr", 1, STR_CURR_NOK }, ///< norwegian krone belugas@6449: { 6, ' ', CF_NOEURO, "", " zl", 1, STR_CURR_PLN }, ///< polish zloty belugas@6449: { 5, '.', CF_NOEURO, "", " Lei", 1, STR_CURR_ROL }, ///< romanian Lei belugas@6449: { 50, ' ', CF_NOEURO, "", " p", 1, STR_CURR_RUR }, ///< russian rouble belugas@6449: { 352, '.', CF_NOEURO, "", " SIT", 1, STR_CURR_SIT }, ///< slovenian tolar belugas@6449: { 13, '.', CF_NOEURO, "", " Kr", 1, STR_CURR_SEK }, ///< swedish krona belugas@6449: { 3, '.', CF_NOEURO, "", " YTL", 1, STR_CURR_YTL }, ///< turkish lira belugas@6449: { 52, ',', CF_NOEURO, "", " Sk", 1, STR_CURR_SKK }, ///< slovak koruna belugas@6449: { 4, ',', CF_NOEURO, "R$ ", "", 0, STR_CURR_BRR }, ///< brazil real belugas@6449: { 1, ' ', CF_NOEURO, "", "", 2, STR_CURR_CUSTOM }, ///< custom currency dominik@759: }; dominik@768: belugas@4377: /* Array of currencies used by the system */ belugas@4377: CurrencySpec _currency_specs[NUM_CURRENCY]; tron@2291: belugas@4625: /** belugas@4625: * These enums are only declared in order to make sens belugas@4625: * out of the TTDPatch_To_OTTDIndex array that will follow belugas@4625: * Every currency used by Ottd is there, just in case TTDPatch will belugas@4625: * add those missing in its code belugas@4625: **/ belugas@4625: enum { belugas@4625: CURR_GBP, belugas@4625: CURR_USD, belugas@4625: CURR_EUR, belugas@4625: CURR_YEN, belugas@4625: CURR_ATS, belugas@4625: CURR_BEF, belugas@4625: CURR_CHF, belugas@4625: CURR_CZK, belugas@4625: CURR_DEM, belugas@4625: CURR_DKK, belugas@4625: CURR_ESP, belugas@4625: CURR_FIM, belugas@4625: CURR_FRF, belugas@4625: CURR_GRD, belugas@4625: CURR_HUF, belugas@4625: CURR_ISK, belugas@4625: CURR_ITL, belugas@4625: CURR_NLG, belugas@4625: CURR_NOK, belugas@4625: CURR_PLN, belugas@4625: CURR_ROL, belugas@4625: CURR_RUR, belugas@4625: CURR_SIT, belugas@4625: CURR_SEK, belugas@4625: CURR_YTL, belugas@4625: }; belugas@4625: belugas@4625: /** belugas@4625: * This array represent the position of OpenTTD's currencies, belugas@4625: * compared to TTDPatch's ones. belugas@4625: * When a grf sends currencies, they are based on the order defined by TTDPatch. belugas@4625: * So, we must reindex them to our own order. belugas@4625: **/ belugas@4625: const byte TTDPatch_To_OTTDIndex[] = belugas@4625: { belugas@4625: CURR_GBP, belugas@4625: CURR_USD, belugas@4625: CURR_FRF, belugas@4625: CURR_DEM, belugas@4625: CURR_YEN, belugas@4625: CURR_ESP, belugas@4625: CURR_HUF, belugas@4625: CURR_PLN, belugas@4625: CURR_ATS, belugas@4625: CURR_BEF, belugas@4625: CURR_DKK, belugas@4625: CURR_FIM, belugas@4625: CURR_GRD, belugas@4625: CURR_CHF, belugas@4625: CURR_NLG, belugas@4625: CURR_ITL, belugas@4625: CURR_SEK, belugas@4625: CURR_RUR, belugas@4625: CURR_EUR, belugas@4625: }; belugas@4625: belugas@4625: /** belugas@4625: * Will return the ottd's index correspondance to belugas@4625: * the ttdpatch's id. If the id is bigger then the array, belugas@4625: * it is a grf written for ottd, thus returning the same id. belugas@4625: * Only called from newgrf.c belugas@4625: * @param grfcurr_id currency id coming from newgrf belugas@4625: * @return the corrected index belugas@4625: **/ belugas@4625: byte GetNewgrfCurrencyIdConverted(byte grfcurr_id) belugas@4625: { glx@4626: return (grfcurr_id >= lengthof(TTDPatch_To_OTTDIndex)) ? grfcurr_id : TTDPatch_To_OTTDIndex[grfcurr_id]; belugas@4625: } belugas@4625: belugas@6449: /** belugas@6449: * get a mask of the allowed currencies depending on the year belugas@6449: * @return mask of currencies belugas@6449: */ rubidium@6573: uint GetMaskOfAllowedCurrencies() tron@2291: { tron@2291: uint mask = 0; tron@2291: uint i; tron@2291: belugas@4377: for (i = 0; i < NUM_CURRENCY; i++) { rubidium@4297: Year to_euro = _currency_specs[i].to_euro; tron@2291: rubidium@4293: if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue; rubidium@4293: if (to_euro == CF_ISEURO && _cur_year < 2000) continue; tron@2291: mask |= (1 << i); tron@2291: } peter1138@3596: mask |= (1 << CUSTOM_CURRENCY_ID); // always allow custom currency tron@2291: return mask; tron@2291: } tron@2291: belugas@4377: /** belugas@4377: * Verify if the currency chosen by the user is about to be converted to Euro belugas@4377: **/ rubidium@6573: void CheckSwitchToEuro() tron@2306: { tron@2306: if (_currency_specs[_opt.currency].to_euro != CF_NOEURO && tron@2306: _currency_specs[_opt.currency].to_euro != CF_ISEURO && rubidium@4293: _cur_year >= _currency_specs[_opt.currency].to_euro) { tron@2306: _opt.currency = 2; // this is the index of euro above. rubidium@9259: AddNewsItem(STR_EURO_INTRODUCE, NM_NORMAL, NF_NONE, NT_ECONOMY, DNC_NONE, 0, 0); tron@2306: } tron@2306: } belugas@4138: belugas@4377: /** belugas@6705: * Will fill _currency_specs array with belugas@4377: * default values from origin_currency_specs belugas@6705: * Called only from newgrf.cpp and settings.cpp. belugas@6705: * @param preserve_custom will not reset custom currency (the latest one on the list) belugas@6705: * if ever it is flagged to true. In which case, the total size of the memory to move belugas@6705: * will be one currency spec less, thus preserving the custom curreny from been belugas@6705: * overwritten. belugas@4377: **/ belugas@6705: void ResetCurrencies(bool preserve_custom) belugas@4377: { belugas@6705: memcpy(&_currency_specs, &origin_currency_specs, sizeof(origin_currency_specs) - (preserve_custom ? sizeof(_custom_currency) : 0)); belugas@4377: } belugas@4377: belugas@4377: /** belugas@4377: * Build a list of currency names StringIDs to use in a dropdown list belugas@4377: * @return Pointer to a (static) array of StringIDs belugas@4377: */ rubidium@6573: StringID* BuildCurrencyDropdown() belugas@4377: { belugas@4377: /* Allow room for all currencies, plus a terminator entry */ rubidium@6552: static StringID names[NUM_CURRENCY + 1]; belugas@4377: uint i; belugas@4377: belugas@4377: /* Add each name */ belugas@4377: for (i = 0; i < NUM_CURRENCY; i++) { belugas@4377: names[i] = _currency_specs[i].name; belugas@4377: } belugas@4377: /* Terminate the list */ belugas@4377: names[i] = INVALID_STRING_ID; belugas@4377: belugas@4377: return names; belugas@4377: }