newgrf_text.c
changeset 3821 57db5bab0f24
parent 3646 291be46d399e
child 3856 00418a7438be
equal deleted inserted replaced
3820:952119a1503d 3821:57db5bab0f24
    11 
    11 
    12 #include "stdafx.h"
    12 #include "stdafx.h"
    13 #include "debug.h"
    13 #include "debug.h"
    14 #include "openttd.h"
    14 #include "openttd.h"
    15 #include "string.h"
    15 #include "string.h"
       
    16 #include "strings.h"
    16 #include "variables.h"
    17 #include "variables.h"
    17 #include "macros.h"
    18 #include "macros.h"
    18 #include "table/strings.h"
    19 #include "table/strings.h"
    19 #include "newgrf_text.h"
    20 #include "newgrf_text.h"
    20 
    21 
   163 
   164 
   164 
   165 
   165 /**
   166 /**
   166  * Add the new read string into our structure.
   167  * Add the new read string into our structure.
   167  */
   168  */
   168 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add)
   169 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
   169 {
   170 {
   170 	GRFText *newtext;
   171 	GRFText *newtext;
   171 	uint id;
   172 	uint id;
   172 
   173 
   173 	/* When working with the old language scheme (grf_version is less than 7) and
   174 	/* When working with the old language scheme (grf_version is less than 7) and
   179 	if (!new_scheme) {
   180 	if (!new_scheme) {
   180 		if (HASBITS(langid_to_add, GRFLB_AMERICAN | GRFLB_ENGLISH)) {
   181 		if (HASBITS(langid_to_add, GRFLB_AMERICAN | GRFLB_ENGLISH)) {
   181 			langid_to_add = GRFLX_ENGLISH;
   182 			langid_to_add = GRFLX_ENGLISH;
   182 		} else {
   183 		} else {
   183 			StringID ret = STR_EMPTY;
   184 			StringID ret = STR_EMPTY;
   184 			if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_GERMAN,  true, text_to_add);
   185 			if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_GERMAN,  true, text_to_add, def_string);
   185 			if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_FRENCH,  true, text_to_add);
   186 			if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_FRENCH,  true, text_to_add, def_string);
   186 			if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_SPANISH, true, text_to_add);
   187 			if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, 1 << 6 | GRFLX_SPANISH, true, text_to_add, def_string);
   187 			return ret;
   188 			return ret;
   188 		}
   189 		}
   189 	}
   190 	}
   190 
   191 
   191 	newtext = calloc(1, sizeof(*newtext));
   192 	newtext = calloc(1, sizeof(*newtext));
   208 	if (id == _num_grf_texts) _num_grf_texts++;
   209 	if (id == _num_grf_texts) _num_grf_texts++;
   209 
   210 
   210 	if (_grf_text[id].textholder == NULL) {
   211 	if (_grf_text[id].textholder == NULL) {
   211 		_grf_text[id].grfid      = grfid;
   212 		_grf_text[id].grfid      = grfid;
   212 		_grf_text[id].stringid   = stringid;
   213 		_grf_text[id].stringid   = stringid;
       
   214 		_grf_text[id].def_string = def_string;
   213 		_grf_text[id].textholder = newtext;
   215 		_grf_text[id].textholder = newtext;
   214 	} else {
   216 	} else {
   215 		GRFText *textptr = _grf_text[id].textholder;
   217 		GRFText *textptr = _grf_text[id].textholder;
   216 		while (textptr->next != NULL) textptr = textptr->next;
   218 		while (textptr->next != NULL) textptr = textptr->next;
   217 		textptr->next = newtext;
   219 		textptr->next = newtext;
   240 
   242 
   241 
   243 
   242 char *GetGRFString(char *buff, uint16 stringid)
   244 char *GetGRFString(char *buff, uint16 stringid)
   243 {
   245 {
   244 	GRFText *search_text;
   246 	GRFText *search_text;
       
   247 	GRFText *default_text = NULL;
   245 
   248 
   246 	assert(_grf_text[stringid].grfid != 0);
   249 	assert(_grf_text[stringid].grfid != 0);
   247 	/*Search the list of lang-strings of this stringid for current lang */
   250 	/*Search the list of lang-strings of this stringid for current lang */
   248 	for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
   251 	for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
   249 		if (search_text->langid == _currentLangID){
   252 		if (search_text->langid == _currentLangID) {
   250 			return strecpy(buff, search_text->text, NULL);
   253 			return strecpy(buff, search_text->text, NULL);
   251 		}
   254 		}
   252 	}
   255 
   253 
   256 		/* If the current string is English or American, set it as the
   254 	/* Use the first text if the specific language isn't available */
   257 		 * fallback language if the specific language isn't available. */
   255 	return strecpy(buff, _grf_text[stringid].textholder->text, NULL);
   258 		if (search_text->langid == GRFLX_ENGLISH || search_text->langid == GRFLX_AMERICAN) {
       
   259 			default_text = search_text;
       
   260 		}
       
   261 	}
       
   262 
       
   263 	/* If there is a fallback string, return that */
       
   264 	if (default_text != NULL) return strecpy(buff, default_text->text, NULL);
       
   265 
       
   266 	/* Use the default string ID if the fallback string isn't available */
       
   267 	return GetString(buff, _grf_text[stringid].def_string);
   256 }
   268 }
   257 
   269 
   258 /**
   270 /**
   259  * Equivalence Setter function between game and newgrf langID.
   271  * Equivalence Setter function between game and newgrf langID.
   260  * This function will adjust _currentLangID as to what is the LangID
   272  * This function will adjust _currentLangID as to what is the LangID