# newgrf_text.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_text.cpp 19585 2010-04-08 15:40:46Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00019 #include "stdafx.h"
    00020 #include "newgrf.h"
    00021 #include "strings_func.h"
    00022 #include "newgrf_storage.h"
    00023 #include "string_func.h"
    00024 #include "date_type.h"
    00025 #include "debug.h"
    00026
    00027 #include "table/strings.h"
    00028 #include "table/control_codes.h"
    00029
    00030 #define GRFTAB  28
    00031 #define TABSIZE 11
    00032
    00040 StringID TTDPStringIDToOTTDStringIDMapping(StringID str)
    00041 {
    00042   /* StringID table for TextIDs 0x4E->0x6D */
    00043   static const StringID units_volume[] = {
    00044     STR_NOTHING,    STR_PASSENGERS, STR_TONS,       STR_BAGS,
    00045     STR_LITERS,     STR_ITEMS,      STR_CRATES,     STR_TONS,
    00046     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
    00047     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
    00048     STR_TONS,       STR_TONS,       STR_BAGS,       STR_LITERS,
    00049     STR_TONS,       STR_LITERS,     STR_TONS,       STR_NOTHING,
    00050     STR_BAGS,       STR_LITERS,     STR_TONS,       STR_NOTHING,
    00051     STR_TONS,       STR_NOTHING,    STR_LITERS,     STR_NOTHING
    00052   };
    00053
    00054   /* A string straight from a NewGRF; no need to remap this as it's already mapped. */
    00055   if (IsInsideMM(str, 0xD000, 0xD7FF) || IsInsideMM(str, 0xDC00, 0xDCFF)) return str;
    00056
    00057 #define TEXTID_TO_STRINGID(begin, end, stringid) if (str >= begin && str <= end) return str + (stringid - begin)
    00058   /* We have some changes in our cargo strings, resulting in some missing. */
    00059   TEXTID_TO_STRINGID(0x000E, 0x002D, STR_CARGO_PLURAL_NOTHING);
    00060   TEXTID_TO_STRINGID(0x002E, 0x004D, STR_CARGO_SINGULAR_NOTHING);
    00061   if (str >= 0x004E && str <= 0x006D) return units_volume[str - 0x004E];
    00062   TEXTID_TO_STRINGID(0x006E, 0x008D, STR_QUANTITY_NOTHING);
    00063   TEXTID_TO_STRINGID(0x008E, 0x00AD, STR_ABBREV_NOTHING);
    00064
    00065   /* Map building names according to our lang file changes. There are several
    00066    * ranges of house ids, all of which need to be remapped to allow newgrfs
    00067    * to use original house names. */
    00068   TEXTID_TO_STRINGID(0x200F, 0x201F, STR_TOWN_BUILDING_NAME_TALL_OFFICE_BLOCK_1);
    00069   TEXTID_TO_STRINGID(0x2036, 0x2041, STR_TOWN_BUILDING_NAME_COTTAGES_1);
    00070   TEXTID_TO_STRINGID(0x2059, 0x205C, STR_TOWN_BUILDING_NAME_IGLOO_1);
    00071
    00072   /* Same thing for industries */
    00073   TEXTID_TO_STRINGID(0x4802, 0x4826, STR_INDUSTRY_NAME_COAL_MINE);
    00074   TEXTID_TO_STRINGID(0x482D, 0x482E, STR_NEWS_INDUSTRY_CONSTRUCTION);
    00075   TEXTID_TO_STRINGID(0x4832, 0x4834, STR_NEWS_INDUSTRY_CLOSURE_GENERAL);
    00076   TEXTID_TO_STRINGID(0x4835, 0x4838, STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_GENERAL);
    00077   TEXTID_TO_STRINGID(0x4839, 0x483A, STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_GENERAL);
    00078
    00079   switch (str) {
    00080     case 0x4830: return STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY;
    00081     case 0x4831: return STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
    00082     case 0x483B: return STR_ERROR_CAN_ONLY_BE_POSITIONED;
    00083   }
    00084 #undef TEXTID_TO_STRINGID
    00085
    00086   if (str == STR_NULL) return STR_EMPTY;
    00087
    00088   DEBUG(grf, 0, "Unknown StringID 0x%04X remapped to STR_EMPTY. Please open a Feature Request if you need it", str);
    00089
    00090   return STR_EMPTY;
    00091 }
    00092
    00098 enum GRFBaseLanguages {
    00099   GRFLB_AMERICAN    = 0x01,
    00100   GRFLB_ENGLISH     = 0x02,
    00101   GRFLB_GERMAN      = 0x04,
    00102   GRFLB_FRENCH      = 0x08,
    00103   GRFLB_SPANISH     = 0x10,
    00104   GRFLB_GENERIC     = 0x80,
    00105 };
    00106
    00107 enum GRFExtendedLanguages {
    00108   GRFLX_AMERICAN    = 0x00,
    00109   GRFLX_ENGLISH     = 0x01,
    00110   GRFLX_GERMAN      = 0x02,
    00111   GRFLX_FRENCH      = 0x03,
    00112   GRFLX_SPANISH     = 0x04,
    00113   GRFLX_UNSPECIFIED = 0x7F,
    00114 };
    00115
    00121 struct GRFText {
    00122 public:
    00123   static GRFText *New(byte langid, const char *text)
    00124   {
    00125     return new (strlen(text) + 1) GRFText(langid, text);
    00126   }
    00127
    00133   void *operator new(size_t size)
    00134   {
    00135     NOT_REACHED();
    00136   }
    00137
    00142   void operator delete(void *p)
    00143   {
    00144     free(p);
    00145   }
    00146 private:
    00147   GRFText(byte langid_, const char *text_) : next(NULL), langid(langid_)
    00148   {
    00149     strcpy(text, text_);
    00150   }
    00151
    00158   void *operator new(size_t size, size_t extra)
    00159   {
    00160     return MallocT<byte>(size + extra);
    00161   }
    00162
    00163 public:
    00164   GRFText *next;
    00165   byte langid;
    00166   char text[];
    00167 };
    00168
    00169
    00175 struct GRFTextEntry {
    00176   uint32 grfid;
    00177   uint16 stringid;
    00178   StringID def_string;
    00179   GRFText *textholder;
    00180 };
    00181
    00182
    00183 static uint _num_grf_texts = 0;
    00184 static GRFTextEntry _grf_text[(1 << TABSIZE) * 3];
    00185 static byte _currentLangID = GRFLX_ENGLISH;
    00186
    00187
    00188 char *TranslateTTDPatchCodes(uint32 grfid, const char *str)
    00189 {
    00190   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
    00191   char *d = tmp;
    00192   bool unicode = false;
    00193   WChar c;
    00194   size_t len = Utf8Decode(&c, str);
    00195
    00196   if (c == 0x00DE) {
    00197     /* The thorn ('þ') indicates a unicode string to TTDPatch */
    00198     unicode = true;
    00199     str += len;
    00200   }
    00201
    00202   for (;;) {
    00203     if (unicode && Utf8EncodedCharLen(*str) != 0) {
    00204       c = Utf8Consume(&str);
    00205       /* 'Magic' range of control codes. */
    00206       if (GB(c, 8, 8) == 0xE0) {
    00207         c = GB(c, 0, 8);
    00208       } else if (c >= 0x20) {
    00209         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
    00210         d += Utf8Encode(d, c);
    00211         continue;
    00212       }
    00213     } else {
    00214       c = (byte)*str++;
    00215     }
    00216     if (c == 0) break;
    00217
    00218     switch (c) {
    00219       case 0x01:
    00220         d += Utf8Encode(d, SCC_SETX);
    00221         *d++ = *str++;
    00222         break;
    00223       case 0x0A: break;
    00224       case 0x0D: *d++ = 0x0A; break;
    00225       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
    00226       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
    00227       case 0x1F:
    00228         d += Utf8Encode(d, SCC_SETXY);
    00229         *d++ = *str++;
    00230         *d++ = *str++;
    00231         break;
    00232       case 0x7B:
    00233       case 0x7C:
    00234       case 0x7D:
    00235       case 0x7E:
    00236       case 0x7F:
    00237       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
    00238       case 0x81: {
    00239         StringID string;
    00240         string  = ((uint8)*str++);
    00241         string |= ((uint8)*str++) << 8;
    00242         d += Utf8Encode(d, SCC_STRING_ID);
    00243         d += Utf8Encode(d, MapGRFStringID(grfid, string));
    00244         break;
    00245       }
    00246       case 0x82:
    00247       case 0x83:
    00248       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_SPEED + c - 0x82); break;
    00249       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
    00250       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
    00251       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_LITRES);  break;
    00252       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
    00253       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
    00254       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
    00255       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
    00256       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
    00257       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
    00258       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
    00259       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
    00260       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
    00261       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
    00262       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
    00263       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
    00264       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
    00265       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
    00266       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
    00267       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
    00268       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
    00269       case 0x9A:
    00270         switch (*str++) {
    00271           case 0: // FALL THROUGH
    00272           case 1:
    00273             d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY);
    00274             break;
    00275           case 3: {
    00276             uint16 tmp  = ((uint8)*str++);
    00277             tmp        |= ((uint8)*str++) << 8;
    00278             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
    00279             d += Utf8Encode(d, tmp);
    00280           } break;
    00281           case 4:
    00282             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
    00283             d += Utf8Encode(d, *str++);
    00284             break;
    00285           case 6:
    00286             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);
    00287             break;
    00288           case 7:
    00289             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);
    00290             break;
    00291           case 8:
    00292             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);
    00293             break;
    00294           case 0x0B:
    00295             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_QWORD);
    00296             break;
    00297
    00298           default:
    00299             grfmsg(1, "missing handler for extended format code");
    00300             break;
    00301         }
    00302         break;
    00303
    00304       case 0x9E: d += Utf8Encode(d, 0x20AC); break; // Euro
    00305       case 0x9F: d += Utf8Encode(d, 0x0178); break; // Y with diaeresis
    00306       case 0xA0: d += Utf8Encode(d, SCC_UPARROW); break;
    00307       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW); break;
    00308       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK); break;
    00309       case 0xAD: d += Utf8Encode(d, SCC_CROSS); break;
    00310       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW); break;
    00311       case 0xB4: d += Utf8Encode(d, SCC_TRAIN); break;
    00312       case 0xB5: d += Utf8Encode(d, SCC_LORRY); break;
    00313       case 0xB6: d += Utf8Encode(d, SCC_BUS); break;
    00314       case 0xB7: d += Utf8Encode(d, SCC_PLANE); break;
    00315       case 0xB8: d += Utf8Encode(d, SCC_SHIP); break;
    00316       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
    00317       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW); break;
    00318       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
    00319       default:
    00320         /* Validate any unhandled character */
    00321         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
    00322         d += Utf8Encode(d, c);
    00323         break;
    00324     }
    00325   }
    00326
    00327   *d = '\0';
    00328   tmp = ReallocT(tmp, strlen(tmp) + 1);
    00329   return tmp;
    00330 }
    00331
    00332
    00336 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
    00337 {
    00338   char *translatedtext;
    00339   uint id;
    00340
    00341   /* When working with the old language scheme (grf_version is less than 7) and
    00342    * English or American is among the set bits, simply add it as English in
    00343    * the new scheme, i.e. as langid = 1.
    00344    * If English is set, it is pretty safe to assume the translations are not
    00345    * actually translated.
    00346    */
    00347   if (!new_scheme) {
    00348     if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
    00349       langid_to_add = GRFLX_ENGLISH;
    00350     } else {
    00351       StringID ret = STR_EMPTY;
    00352       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
    00353       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
    00354       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
    00355       return ret;
    00356     }
    00357   }
    00358
    00359   for (id = 0; id < _num_grf_texts; id++) {
    00360     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
    00361       break;
    00362     }
    00363   }
    00364
    00365   /* Too many strings allocated, return empty */
    00366   if (id == lengthof(_grf_text)) return STR_EMPTY;
    00367
    00368   translatedtext = TranslateTTDPatchCodes(grfid, text_to_add);
    00369
    00370   GRFText *newtext = GRFText::New(langid_to_add, translatedtext);
    00371
    00372   free(translatedtext);
    00373
    00374   /* If we didn't find our stringid and grfid in the list, allocate a new id */
    00375   if (id == _num_grf_texts) _num_grf_texts++;
    00376
    00377   if (_grf_text[id].textholder == NULL) {
    00378     _grf_text[id].grfid      = grfid;
    00379     _grf_text[id].stringid   = stringid;
    00380     _grf_text[id].def_string = def_string;
    00381     _grf_text[id].textholder = newtext;
    00382   } else {
    00383     GRFText **ptext, *text;
    00384     bool replaced = false;
    00385
    00386     /* Loop through all languages and see if we can replace a string */
    00387     for (ptext = &_grf_text[id].textholder; (text = *ptext) != NULL; ptext = &text->next) {
    00388       if (text->langid != langid_to_add) continue;
    00389       newtext->next = text->next;
    00390       *ptext = newtext;
    00391       delete text;
    00392       replaced = true;
    00393       break;
    00394     }
    00395
    00396     /* If a string wasn't replaced, then we must append the new string */
    00397     if (!replaced) *ptext = newtext;
    00398   }
    00399
    00400   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
    00401
    00402   return (GRFTAB << TABSIZE) + id;
    00403 }
    00404
    00405 /* Used to remember the grfid that the last retrieved string came from */
    00406 static uint32 _last_grfid = 0;
    00407
    00411 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
    00412 {
    00413   uint id;
    00414
    00415   /* grfid is zero when we're being called via an include */
    00416   if (grfid == 0) grfid = _last_grfid;
    00417
    00418   for (id = 0; id < _num_grf_texts; id++) {
    00419     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
    00420       return (GRFTAB << TABSIZE) + id;
    00421     }
    00422   }
    00423
    00424   return STR_UNDEFINED;
    00425 }
    00426
    00427
    00428 const char *GetGRFStringPtr(uint16 stringid)
    00429 {
    00430   const GRFText *default_text = NULL;
    00431   const GRFText *search_text;
    00432
    00433   assert(_grf_text[stringid].grfid != 0);
    00434
    00435   /* Remember this grfid in case the string has included text */
    00436   _last_grfid = _grf_text[stringid].grfid;
    00437
    00438   /* Search the list of lang-strings of this stringid for current lang */
    00439   for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
    00440     if (search_text->langid == _currentLangID) {
    00441       return search_text->text;
    00442     }
    00443
    00444     /* If the current string is English or American, set it as the
    00445      * fallback language if the specific language isn't available. */
    00446     if (search_text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (search_text->langid == GRFLX_ENGLISH || search_text->langid == GRFLX_AMERICAN))) {
    00447       default_text = search_text;
    00448     }
    00449   }
    00450
    00451   /* If there is a fallback string, return that */
    00452   if (default_text != NULL) return default_text->text;
    00453
    00454   /* Use the default string ID if the fallback string isn't available */
    00455   return GetStringPtr(_grf_text[stringid].def_string);
    00456 }
    00457
    00466 void SetCurrentGrfLangID(byte language_id)
    00467 {
    00468   _currentLangID = language_id;
    00469 }
    00470
    00471 bool CheckGrfLangID(byte lang_id, byte grf_version)
    00472 {
    00473   if (grf_version < 7) {
    00474     switch (_currentLangID) {
    00475       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
    00476       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
    00477       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
    00478       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
    00479     }
    00480   }
    00481
    00482   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
    00483 }
    00484
    00489 void CleanUpStrings()
    00490 {
    00491   uint id;
    00492
    00493   for (id = 0; id < _num_grf_texts; id++) {
    00494     GRFText *grftext = _grf_text[id].textholder;
    00495     while (grftext != NULL) {
    00496       GRFText *grftext2 = grftext->next;
    00497       delete grftext;
    00498       grftext = grftext2;
    00499     }
    00500     _grf_text[id].grfid      = 0;
    00501     _grf_text[id].stringid   = 0;
    00502     _grf_text[id].textholder = NULL;
    00503   }
    00504
    00505   _num_grf_texts = 0;
    00506 }
    00507
    00508 struct TextRefStack {
    00509   byte stack[0x30];
    00510   byte position;
    00511   bool used;
    00512
    00513   TextRefStack() : used(false) {}
    00514
    00515   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
    00516   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
    00517
    00518   uint16 PopUnsignedWord()
    00519   {
    00520     uint16 val = this->PopUnsignedByte();
    00521     return val | (this->PopUnsignedByte() << 8);
    00522   }
    00523   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
    00524
    00525   uint32 PopUnsignedDWord()
    00526   {
    00527     uint32 val = this->PopUnsignedWord();
    00528     return val | (this->PopUnsignedWord() << 16);
    00529   }
    00530   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
    00531
    00532   uint64 PopUnsignedQWord()
    00533   {
    00534     uint64 val = this->PopUnsignedDWord();
    00535     return val | (((uint64)this->PopUnsignedDWord()) << 32);
    00536   }
    00537   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
    00538
    00540   void RotateTop4Words()
    00541   {
    00542     byte tmp[2];
    00543     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
    00544     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
    00545     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
    00546   }
    00547
    00548   void PushWord(uint16 word)
    00549   {
    00550     if (this->position >= 2) {
    00551       this->position -= 2;
    00552     } else {
    00553       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
    00554         this->stack[i] = this->stack[i - 2];
    00555       }
    00556     }
    00557     this->stack[this->position]     = GB(word, 0, 8);
    00558     this->stack[this->position + 1] = GB(word, 8, 8);
    00559   }
    00560
    00561   void ResetStack()  { this->position = 0; this->used = true; }
    00562   void RewindStack() { this->position = 0; }
    00563 };
    00564
    00565 static TextRefStack _newgrf_normal_textrefstack;
    00566 static TextRefStack _newgrf_error_textrefstack;
    00567
    00569 static TextRefStack *_newgrf_textrefstack = &_newgrf_normal_textrefstack;
    00570
    00575 void PrepareTextRefStackUsage(byte numEntries)
    00576 {
    00577   extern TemporaryStorageArray<int32, 0x110> _temp_store;
    00578
    00579   _newgrf_textrefstack->ResetStack();
    00580
    00581   byte *p = _newgrf_textrefstack->stack;
    00582   for (uint i = 0; i < numEntries; i++) {
    00583     for (uint j = 0; j < 32; j += 8) {
    00584       *p = GB(_temp_store.Get(0x100 + i), j, 8);
    00585       p++;
    00586     }
    00587   }
    00588 }
    00589
    00591 void StopTextRefStackUsage() { _newgrf_textrefstack->used = false; }
    00592
    00593 void SwitchToNormalRefStack()
    00594 {
    00595   _newgrf_textrefstack = &_newgrf_normal_textrefstack;
    00596 }
    00597
    00598 void SwitchToErrorRefStack()
    00599 {
    00600   _newgrf_textrefstack = &_newgrf_error_textrefstack;
    00601 }
    00602
    00603 void RewindTextRefStack()
    00604 {
    00605   _newgrf_textrefstack->RewindStack();
    00606 }
    00607
    00616 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv)
    00617 {
    00618   if (_newgrf_textrefstack->used) {
    00619     switch (scc) {
    00620       default: NOT_REACHED();
    00621       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
    00622       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
    00623       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack->PopUnsignedQWord(); break;
    00624
    00625       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
    00626       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack->PopSignedDWord();   break;
    00627
    00628       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack->PopUnsignedByte();  break;
    00629       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack->PopUnsignedDWord(); break;
    00630       case SCC_NEWGRF_PRINT_HEX_QWORD:      *argv = _newgrf_textrefstack->PopSignedQWord(); break;
    00631
    00632       case SCC_NEWGRF_PRINT_HEX_WORD:
    00633       case SCC_NEWGRF_PRINT_WORD_SPEED:
    00634       case SCC_NEWGRF_PRINT_WORD_LITRES:
    00635       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack->PopUnsignedWord();  break;
    00636
    00637       case SCC_NEWGRF_PRINT_DATE:
    00638       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
    00639
    00640       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
    00641
    00642       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
    00643       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
    00644       case SCC_NEWGRF_UNPRINT:              *buff = max(*buff - Utf8Consume(str), buf_start); break;
    00645
    00646       case SCC_NEWGRF_PRINT_STRING_ID:
    00647         *argv = TTDPStringIDToOTTDStringIDMapping(_newgrf_textrefstack->PopUnsignedWord());
    00648         break;
    00649     }
    00650   }
    00651
    00652   switch (scc) {
    00653     default: NOT_REACHED();
    00654     case SCC_NEWGRF_PRINT_DWORD:
    00655     case SCC_NEWGRF_PRINT_SIGNED_WORD:
    00656     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
    00657     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
    00658       return SCC_COMMA;
    00659
    00660     case SCC_NEWGRF_PRINT_HEX_BYTE:
    00661     case SCC_NEWGRF_PRINT_HEX_WORD:
    00662     case SCC_NEWGRF_PRINT_HEX_DWORD:
    00663     case SCC_NEWGRF_PRINT_HEX_QWORD:
    00664       return SCC_HEX;
    00665
    00666     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
    00667     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
    00668       return SCC_CURRENCY;
    00669
    00670     case SCC_NEWGRF_PRINT_STRING_ID:
    00671       return SCC_STRING1;
    00672
    00673     case SCC_NEWGRF_PRINT_DATE:
    00674       return SCC_DATE_LONG;
    00675
    00676     case SCC_NEWGRF_PRINT_MONTH_YEAR:
    00677       return SCC_DATE_TINY;
    00678
    00679     case SCC_NEWGRF_PRINT_WORD_SPEED:
    00680       return SCC_VELOCITY;
    00681
    00682     case SCC_NEWGRF_PRINT_WORD_LITRES:
    00683       return SCC_VOLUME;
    00684
    00685     case SCC_NEWGRF_DISCARD_WORD:
    00686     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
    00687     case SCC_NEWGRF_PUSH_WORD:
    00688     case SCC_NEWGRF_UNPRINT:
    00689       return 0;
    00690   }
    00691 }