settings.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00026 #include "stdafx.h"
00027 #include "currency.h"
00028 #include "screenshot.h"
00029 #include "network/network.h"
00030 #include "network/network_func.h"
00031 #include "settings_internal.h"
00032 #include "command_func.h"
00033 #include "console_func.h"
00034 #include "pathfinder/pathfinder_type.h"
00035 #include "genworld.h"
00036 #include "train.h"
00037 #include "news_func.h"
00038 #include "window_func.h"
00039 #include "sound_func.h"
00040 #include "company_func.h"
00041 #include "rev.h"
00042 #ifdef WITH_FREETYPE
00043 #include "fontcache.h"
00044 #endif
00045 #include "textbuf_gui.h"
00046 #include "rail_gui.h"
00047 #include "elrail_func.h"
00048 #include "error.h"
00049 #include "town.h"
00050 #include "video/video_driver.hpp"
00051 #include "sound/sound_driver.hpp"
00052 #include "music/music_driver.hpp"
00053 #include "blitter/factory.hpp"
00054 #include "base_media_base.h"
00055 #include "gamelog.h"
00056 #include "settings_func.h"
00057 #include "ini_type.h"
00058 #include "ai/ai_config.hpp"
00059 #include "ai/ai.hpp"
00060 #include "ship.h"
00061 #include "smallmap_gui.h"
00062 #include "roadveh.h"
00063 #include "fios.h"
00064 #include "strings_func.h"
00065 
00066 #include "void_map.h"
00067 #include "station_base.h"
00068 
00069 #include "table/strings.h"
00070 #include "table/settings.h"
00071 
00072 ClientSettings _settings_client;
00073 GameSettings _settings_game;     
00074 GameSettings _settings_newgame;  
00075 VehicleDefaultSettings _old_vds; 
00076 char *_config_file; 
00077 
00078 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00079 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00080 
00081 static bool IsSignedVarMemType(VarType vt);
00082 
00086 static const char * const _list_group_names[] = {
00087   "bans",
00088   "newgrf",
00089   "servers",
00090   "server_bind_addresses",
00091   NULL
00092 };
00093 
00101 static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00102 {
00103   const char *s;
00104   size_t idx;
00105 
00106   if (onelen == 0) onelen = strlen(one);
00107 
00108   /* check if it's an integer */
00109   if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00110 
00111   idx = 0;
00112   for (;;) {
00113     /* find end of item */
00114     s = many;
00115     while (*s != '|' && *s != 0) s++;
00116     if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00117     if (*s == 0) return (size_t)-1;
00118     many = s + 1;
00119     idx++;
00120   }
00121 }
00122 
00130 static size_t LookupManyOfMany(const char *many, const char *str)
00131 {
00132   const char *s;
00133   size_t r;
00134   size_t res = 0;
00135 
00136   for (;;) {
00137     /* skip "whitespace" */
00138     while (*str == ' ' || *str == '\t' || *str == '|') str++;
00139     if (*str == 0) break;
00140 
00141     s = str;
00142     while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00143 
00144     r = LookupOneOfMany(many, str, s - str);
00145     if (r == (size_t)-1) return r;
00146 
00147     SetBit(res, (uint8)r); // value found, set it
00148     if (*s == 0) break;
00149     str = s + 1;
00150   }
00151   return res;
00152 }
00153 
00162 static int ParseIntList(const char *p, int *items, int maxitems)
00163 {
00164   int n = 0; // number of items read so far
00165   bool comma = false; // do we accept comma?
00166 
00167   while (*p != '\0') {
00168     switch (*p) {
00169       case ',':
00170         /* Do not accept multiple commas between numbers */
00171         if (!comma) return -1;
00172         comma = false;
00173         /* FALL THROUGH */
00174       case ' ':
00175         p++;
00176         break;
00177 
00178       default: {
00179         if (n == maxitems) return -1; // we don't accept that many numbers
00180         char *end;
00181         long v = strtol(p, &end, 0);
00182         if (p == end) return -1; // invalid character (not a number)
00183         if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00184         items[n++] = v;
00185         p = end; // first non-number
00186         comma = true; // we accept comma now
00187         break;
00188       }
00189     }
00190   }
00191 
00192   /* If we have read comma but no number after it, fail.
00193    * We have read comma when (n != 0) and comma is not allowed */
00194   if (n != 0 && !comma) return -1;
00195 
00196   return n;
00197 }
00198 
00207 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00208 {
00209   int items[64];
00210   int i, nitems;
00211 
00212   if (str == NULL) {
00213     memset(items, 0, sizeof(items));
00214     nitems = nelems;
00215   } else {
00216     nitems = ParseIntList(str, items, lengthof(items));
00217     if (nitems != nelems) return false;
00218   }
00219 
00220   switch (type) {
00221   case SLE_VAR_BL:
00222   case SLE_VAR_I8:
00223   case SLE_VAR_U8:
00224     for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00225     break;
00226   case SLE_VAR_I16:
00227   case SLE_VAR_U16:
00228     for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00229     break;
00230   case SLE_VAR_I32:
00231   case SLE_VAR_U32:
00232     for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00233     break;
00234   default: NOT_REACHED();
00235   }
00236 
00237   return true;
00238 }
00239 
00249 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00250 {
00251   int i, v = 0;
00252   const byte *p = (const byte *)array;
00253 
00254   for (i = 0; i != nelems; i++) {
00255     switch (type) {
00256     case SLE_VAR_BL:
00257     case SLE_VAR_I8:  v = *(const   int8 *)p; p += 1; break;
00258     case SLE_VAR_U8:  v = *(const  uint8 *)p; p += 1; break;
00259     case SLE_VAR_I16: v = *(const  int16 *)p; p += 2; break;
00260     case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
00261     case SLE_VAR_I32: v = *(const  int32 *)p; p += 4; break;
00262     case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
00263     default: NOT_REACHED();
00264     }
00265     buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00266   }
00267 }
00268 
00276 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00277 {
00278   int orig_id = id;
00279 
00280   /* Look for the id'th element */
00281   while (--id >= 0) {
00282     for (; *many != '|'; many++) {
00283       if (*many == '\0') { // not found
00284         seprintf(buf, last, "%d", orig_id);
00285         return;
00286       }
00287     }
00288     many++; // pass the |-character
00289   }
00290 
00291   /* copy string until next item (|) or the end of the list if this is the last one */
00292   while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00293   *buf = '\0';
00294 }
00295 
00304 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00305 {
00306   const char *start;
00307   int i = 0;
00308   bool init = true;
00309 
00310   for (; x != 0; x >>= 1, i++) {
00311     start = many;
00312     while (*many != 0 && *many != '|') many++; // advance to the next element
00313 
00314     if (HasBit(x, 0)) { // item found, copy it
00315       if (!init) buf += seprintf(buf, last, "|");
00316       init = false;
00317       if (start == many) {
00318         buf += seprintf(buf, last, "%d", i);
00319       } else {
00320         memcpy(buf, start, many - start);
00321         buf += many - start;
00322       }
00323     }
00324 
00325     if (*many == '|') many++;
00326   }
00327 
00328   *buf = '\0';
00329 }
00330 
00337 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00338 {
00339   const char *str = orig_str == NULL ? "" : orig_str;
00340   switch (desc->cmd) {
00341   case SDT_NUMX: {
00342     char *end;
00343     size_t val = strtoul(str, &end, 0);
00344     if (*end != '\0') {
00345       SetDParamStr(0, desc->name);
00346       ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS, WL_CRITICAL);
00347     }
00348     return (void*)val;
00349   }
00350   case SDT_ONEOFMANY: {
00351     size_t r = LookupOneOfMany(desc->many, str);
00352     /* if the first attempt of conversion from string to the appropriate value fails,
00353      * look if we have defined a converter from old value to new value. */
00354     if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00355     if (r != (size_t)-1) return (void*)r; // and here goes converted value
00356 
00357     SetDParamStr(0, str);
00358     SetDParamStr(1, desc->name);
00359     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
00360     return 0;
00361   }
00362   case SDT_MANYOFMANY: {
00363     size_t r = LookupManyOfMany(desc->many, str);
00364     if (r != (size_t)-1) return (void*)r;
00365     SetDParamStr(0, str);
00366     SetDParamStr(1, desc->name);
00367     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
00368     return NULL;
00369   }
00370   case SDT_BOOLX:
00371     if (strcmp(str, "true")  == 0 || strcmp(str, "on")  == 0 || strcmp(str, "1") == 0) return (void*)true;
00372     if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00373 
00374     SetDParamStr(0, str);
00375     SetDParamStr(1, desc->name);
00376     ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE, WL_CRITICAL);
00377     break;
00378 
00379   case SDT_STRING: return orig_str;
00380   case SDT_INTLIST: return str;
00381   default: break;
00382   }
00383 
00384   return NULL;
00385 }
00386 
00396 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00397 {
00398   const SettingDescBase *sdb = &sd->desc;
00399 
00400   if (sdb->cmd != SDT_BOOLX &&
00401       sdb->cmd != SDT_NUMX &&
00402       sdb->cmd != SDT_ONEOFMANY &&
00403       sdb->cmd != SDT_MANYOFMANY) {
00404     return;
00405   }
00406 
00407   /* We cannot know the maximum value of a bitset variable, so just have faith */
00408   if (sdb->cmd != SDT_MANYOFMANY) {
00409     /* We need to take special care of the uint32 type as we receive from the function
00410      * a signed integer. While here also bail out on 64-bit settings as those are not
00411      * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
00412      * 32-bit variable
00413      * TODO: Support 64-bit settings/variables */
00414     switch (GetVarMemType(sd->save.conv)) {
00415       case SLE_VAR_NULL: return;
00416       case SLE_VAR_BL:
00417       case SLE_VAR_I8:
00418       case SLE_VAR_U8:
00419       case SLE_VAR_I16:
00420       case SLE_VAR_U16:
00421       case SLE_VAR_I32: {
00422         /* Override the minimum value. No value below sdb->min, except special value 0 */
00423         if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00424         break;
00425       }
00426       case SLE_VAR_U32: {
00427         /* Override the minimum value. No value below sdb->min, except special value 0 */
00428         uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00429         WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00430         return;
00431       }
00432       case SLE_VAR_I64:
00433       case SLE_VAR_U64:
00434       default: NOT_REACHED();
00435     }
00436   }
00437 
00438   WriteValue(ptr, sd->save.conv, (int64)val);
00439 }
00440 
00449 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00450 {
00451   IniGroup *group;
00452   IniGroup *group_def = ini->GetGroup(grpname);
00453   IniItem *item;
00454   const void *p;
00455   void *ptr;
00456   const char *s;
00457 
00458   for (; sd->save.cmd != SL_END; sd++) {
00459     const SettingDescBase *sdb = &sd->desc;
00460     const SaveLoad        *sld = &sd->save;
00461 
00462     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00463 
00464     /* For settings.xx.yy load the settings from [xx] yy = ? */
00465     s = strchr(sdb->name, '.');
00466     if (s != NULL) {
00467       group = ini->GetGroup(sdb->name, s - sdb->name);
00468       s++;
00469     } else {
00470       s = sdb->name;
00471       group = group_def;
00472     }
00473 
00474     item = group->GetItem(s, false);
00475     if (item == NULL && group != group_def) {
00476       /* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
00477        * did not exist (e.g. loading old config files with a [settings] section */
00478       item = group_def->GetItem(s, false);
00479     }
00480     if (item == NULL) {
00481       /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
00482        * did not exist (e.g. loading old config files with a [yapf] section */
00483       const char *sc = strchr(s, '.');
00484       if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00485     }
00486 
00487     p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00488     ptr = GetVariableAddress(object, sld);
00489 
00490     switch (sdb->cmd) {
00491     case SDT_BOOLX: // All four are various types of (integer) numbers
00492     case SDT_NUMX:
00493     case SDT_ONEOFMANY:
00494     case SDT_MANYOFMANY:
00495       Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00496 
00497     case SDT_STRING:
00498       switch (GetVarMemType(sld->conv)) {
00499         case SLE_VAR_STRB:
00500         case SLE_VAR_STRBQ:
00501           if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00502           break;
00503         case SLE_VAR_STR:
00504         case SLE_VAR_STRQ:
00505           free(*(char**)ptr);
00506           *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00507           break;
00508         case SLE_VAR_CHAR: if (p != NULL) *(char *)ptr = *(const char *)p; break;
00509         default: NOT_REACHED();
00510       }
00511       break;
00512 
00513     case SDT_INTLIST: {
00514       if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00515         SetDParamStr(0, sdb->name);
00516         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
00517       } else if (sd->desc.proc_cnvt != NULL) {
00518         sd->desc.proc_cnvt((const char*)p);
00519       }
00520       break;
00521     }
00522     default: NOT_REACHED();
00523     }
00524   }
00525 }
00526 
00539 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00540 {
00541   IniGroup *group_def = NULL, *group;
00542   IniItem *item;
00543   char buf[512];
00544   const char *s;
00545   void *ptr;
00546 
00547   for (; sd->save.cmd != SL_END; sd++) {
00548     const SettingDescBase *sdb = &sd->desc;
00549     const SaveLoad        *sld = &sd->save;
00550 
00551     /* If the setting is not saved to the configuration
00552      * file, just continue with the next setting */
00553     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00554     if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00555 
00556     /* XXX - wtf is this?? (group override?) */
00557     s = strchr(sdb->name, '.');
00558     if (s != NULL) {
00559       group = ini->GetGroup(sdb->name, s - sdb->name);
00560       s++;
00561     } else {
00562       if (group_def == NULL) group_def = ini->GetGroup(grpname);
00563       s = sdb->name;
00564       group = group_def;
00565     }
00566 
00567     item = group->GetItem(s, true);
00568     ptr = GetVariableAddress(object, sld);
00569 
00570     if (item->value != NULL) {
00571       /* check if the value is the same as the old value */
00572       const void *p = StringToVal(sdb, item->value);
00573 
00574       /* The main type of a variable/setting is in bytes 8-15
00575        * The subtype (what kind of numbers do we have there) is in 0-7 */
00576       switch (sdb->cmd) {
00577       case SDT_BOOLX:
00578       case SDT_NUMX:
00579       case SDT_ONEOFMANY:
00580       case SDT_MANYOFMANY:
00581         switch (GetVarMemType(sld->conv)) {
00582         case SLE_VAR_BL:
00583           if (*(bool*)ptr == (p != NULL)) continue;
00584           break;
00585         case SLE_VAR_I8:
00586         case SLE_VAR_U8:
00587           if (*(byte*)ptr == (byte)(size_t)p) continue;
00588           break;
00589         case SLE_VAR_I16:
00590         case SLE_VAR_U16:
00591           if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00592           break;
00593         case SLE_VAR_I32:
00594         case SLE_VAR_U32:
00595           if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00596           break;
00597         default: NOT_REACHED();
00598         }
00599         break;
00600       default: break; // Assume the other types are always changed
00601       }
00602     }
00603 
00604     /* Value has changed, get the new value and put it into a buffer */
00605     switch (sdb->cmd) {
00606     case SDT_BOOLX:
00607     case SDT_NUMX:
00608     case SDT_ONEOFMANY:
00609     case SDT_MANYOFMANY: {
00610       uint32 i = (uint32)ReadValue(ptr, sld->conv);
00611 
00612       switch (sdb->cmd) {
00613       case SDT_BOOLX:      strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00614       case SDT_NUMX:       seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00615       case SDT_ONEOFMANY:  MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00616       case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00617       default: NOT_REACHED();
00618       }
00619       break;
00620     }
00621 
00622     case SDT_STRING:
00623       switch (GetVarMemType(sld->conv)) {
00624       case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00625       case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00626       case SLE_VAR_STR:  strecpy(buf, *(char**)ptr, lastof(buf)); break;
00627       case SLE_VAR_STRQ:
00628         if (*(char**)ptr == NULL) {
00629           buf[0] = '\0';
00630         } else {
00631           seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00632         }
00633         break;
00634       case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00635       default: NOT_REACHED();
00636       }
00637       break;
00638 
00639     case SDT_INTLIST:
00640       MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00641       break;
00642     default: NOT_REACHED();
00643     }
00644 
00645     /* The value is different, that means we have to write it to the ini */
00646     free(item->value);
00647     item->value = strdup(buf);
00648   }
00649 }
00650 
00660 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00661 {
00662   IniGroup *group = ini->GetGroup(grpname);
00663 
00664   if (group == NULL || list == NULL) return;
00665 
00666   list->Clear();
00667 
00668   for (const IniItem *item = group->item; item != NULL; item = item->next) {
00669     if (item->name != NULL) *list->Append() = strdup(item->name);
00670   }
00671 }
00672 
00682 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00683 {
00684   IniGroup *group = ini->GetGroup(grpname);
00685 
00686   if (group == NULL || list == NULL) return;
00687   group->Clear();
00688 
00689   for (char **iter = list->Begin(); iter != list->End(); iter++) {
00690     group->GetItem(*iter, true)->SetValue("");
00691   }
00692 }
00693 
00694 /* Begin - Callback Functions for the various settings. */
00695 
00697 static bool v_PositionMainToolbar(int32 p1)
00698 {
00699   if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00700   return true;
00701 }
00702 
00704 static bool v_PositionStatusbar(int32 p1)
00705 {
00706   if (_game_mode != GM_MENU) {
00707     PositionStatusbar(NULL);
00708     PositionNewsMessage(NULL);
00709     PositionNetworkChatWindow(NULL);
00710   }
00711   return true;
00712 }
00713 
00714 static bool PopulationInLabelActive(int32 p1)
00715 {
00716   UpdateAllTownVirtCoords();
00717   return true;
00718 }
00719 
00720 static bool RedrawScreen(int32 p1)
00721 {
00722   MarkWholeScreenDirty();
00723   return true;
00724 }
00725 
00731 static bool RedrawSmallmap(int32 p1)
00732 {
00733   BuildLandLegend();
00734   BuildOwnerLegend();
00735   SetWindowClassesDirty(WC_SMALLMAP);
00736   return true;
00737 }
00738 
00739 static bool InvalidateDetailsWindow(int32 p1)
00740 {
00741   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00742   return true;
00743 }
00744 
00745 static bool InvalidateStationBuildWindow(int32 p1)
00746 {
00747   SetWindowDirty(WC_BUILD_STATION, 0);
00748   return true;
00749 }
00750 
00751 static bool InvalidateBuildIndustryWindow(int32 p1)
00752 {
00753   InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00754   return true;
00755 }
00756 
00757 static bool CloseSignalGUI(int32 p1)
00758 {
00759   if (p1 == 0) {
00760     DeleteWindowByClass(WC_BUILD_SIGNAL);
00761   }
00762   return true;
00763 }
00764 
00765 static bool InvalidateTownViewWindow(int32 p1)
00766 {
00767   InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00768   return true;
00769 }
00770 
00771 static bool DeleteSelectStationWindow(int32 p1)
00772 {
00773   DeleteWindowById(WC_SELECT_STATION, 0);
00774   return true;
00775 }
00776 
00777 static bool UpdateConsists(int32 p1)
00778 {
00779   Train *t;
00780   FOR_ALL_TRAINS(t) {
00781     /* Update the consist of all trains so the maximum speed is set correctly. */
00782     if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00783   }
00784   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00785   return true;
00786 }
00787 
00788 /* Check service intervals of vehicles, p1 is value of % or day based servicing */
00789 static bool CheckInterval(int32 p1)
00790 {
00791   VehicleDefaultSettings *vds;
00792   if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00793     vds = &_settings_client.company.vehicle;
00794   } else {
00795     vds = &Company::Get(_current_company)->settings.vehicle;
00796   }
00797 
00798   if (p1 != 0) {
00799     vds->servint_trains   = 50;
00800     vds->servint_roadveh  = 50;
00801     vds->servint_aircraft = 50;
00802     vds->servint_ships    = 50;
00803   } else {
00804     vds->servint_trains   = 150;
00805     vds->servint_roadveh  = 150;
00806     vds->servint_aircraft = 100;
00807     vds->servint_ships    = 360;
00808   }
00809 
00810   InvalidateDetailsWindow(0);
00811 
00812   return true;
00813 }
00814 
00815 static bool TrainAccelerationModelChanged(int32 p1)
00816 {
00817   Train *t;
00818   FOR_ALL_TRAINS(t) {
00819     if (t->IsFrontEngine()) {
00820       t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00821       t->UpdateAcceleration();
00822     }
00823   }
00824 
00825   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00826   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00827   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00828   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00829 
00830   return true;
00831 }
00832 
00838 static bool TrainSlopeSteepnessChanged(int32 p1)
00839 {
00840   Train *t;
00841   FOR_ALL_TRAINS(t) {
00842     if (t->IsFrontEngine()) t->CargoChanged();
00843   }
00844 
00845   return true;
00846 }
00847 
00853 static bool RoadVehAccelerationModelChanged(int32 p1)
00854 {
00855   if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00856     RoadVehicle *rv;
00857     FOR_ALL_ROADVEHICLES(rv) {
00858       if (rv->IsFrontEngine()) {
00859         rv->CargoChanged();
00860       }
00861     }
00862   }
00863 
00864   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00865   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00866   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00867   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00868 
00869   return true;
00870 }
00871 
00877 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00878 {
00879   RoadVehicle *rv;
00880   FOR_ALL_ROADVEHICLES(rv) {
00881     if (rv->IsFrontEngine()) rv->CargoChanged();
00882   }
00883 
00884   return true;
00885 }
00886 
00887 static bool DragSignalsDensityChanged(int32)
00888 {
00889   InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00890 
00891   return true;
00892 }
00893 
00894 static bool TownFoundingChanged(int32 p1)
00895 {
00896   if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00897     DeleteWindowById(WC_FOUND_TOWN, 0);
00898     return true;
00899   }
00900   InvalidateWindowData(WC_FOUND_TOWN, 0);
00901   return true;
00902 }
00903 
00904 static bool InvalidateVehTimetableWindow(int32 p1)
00905 {
00906   InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00907   return true;
00908 }
00909 
00910 static bool ZoomMinMaxChanged(int32 p1)
00911 {
00912   extern void ConstrainAllViewportsZoom();
00913   ConstrainAllViewportsZoom();
00914   GfxClearSpriteCache();
00915   return true;
00916 }
00917 
00925 static bool InvalidateNewGRFChangeWindows(int32 p1)
00926 {
00927   InvalidateWindowClassesData(WC_SAVELOAD);
00928   DeleteWindowByClass(WC_GAME_OPTIONS);
00929   ReInitAllWindows();
00930   return true;
00931 }
00932 
00933 static bool InvalidateCompanyLiveryWindow(int32 p1)
00934 {
00935   InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00936   return RedrawScreen(p1);
00937 }
00938 
00939 static bool InvalidateIndustryViewWindow(int32 p1)
00940 {
00941   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00942   return true;
00943 }
00944 
00945 static bool InvalidateAISettingsWindow(int32 p1)
00946 {
00947   InvalidateWindowClassesData(WC_AI_SETTINGS);
00948   return true;
00949 }
00950 
00956 static bool RedrawTownAuthority(int32 p1)
00957 {
00958   SetWindowClassesDirty(WC_TOWN_AUTHORITY);
00959   return true;
00960 }
00961 
00967 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
00968 {
00969   InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
00970   return true;
00971 }
00972 
00973 /*
00974  * A: competitors
00975  * B: competitor start time. Deprecated since savegame version 110.
00976  * C: town count (3 = high, 0 = very low)
00977  * D: industry count (4 = high, 0 = none)
00978  * E: inital loan (in GBP)
00979  * F: interest rate
00980  * G: running costs (0 = low, 2 = high)
00981  * H: construction speed of competitors (0 = very slow, 4 = very fast)
00982  * I: competitor intelligence. Deprecated since savegame version 110.
00983  * J: breakdowns (0 = off, 2 = normal)
00984  * K: subsidy multiplier (0 = 1.5, 3 = 4.0)
00985  * L: construction cost (0-2)
00986  * M: terrain type (0 = very flat, 3 = mountainous)
00987  * N: amount of water (0 = very low, 3 = high)
00988  * O: economy (0 = steady, 1 = fluctuating)
00989  * P: Train reversing (0 = end of line + stations, 1 = end of line)
00990  * Q: disasters
00991  * R: area restructuring (0 = permissive, 2 = hostile)
00992  * S: the difficulty level
00993  */
00994 static const DifficultySettings _default_game_diff[3] = { /*
00995    A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
00996   {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0}, 
00997   {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1}, 
00998   {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2}, 
00999 };
01000 
01001 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
01002 {
01003   assert(mode <= 3);
01004 
01005   if (mode != 3) {
01006     *gm_opt = _default_game_diff[mode];
01007   } else {
01008     gm_opt->diff_level = 3;
01009   }
01010 }
01011 
01013 static void ValidateSettings()
01014 {
01015   /* Force the difficulty levels to correct values if they are invalid. */
01016   if (_settings_newgame.difficulty.diff_level != 3) {
01017     SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
01018   }
01019 
01020   /* Do not allow a custom sea level with the original land generator. */
01021   if (_settings_newgame.game_creation.land_generator == 0 &&
01022       _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
01023     _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
01024   }
01025 }
01026 
01027 static bool DifficultyReset(int32 level)
01028 {
01029   /* In game / in the scenario editor you can set the difficulty level only to custom. This is
01030    * needed by the AI Gui code that sets the difficulty level when you change any AI settings. */
01031   if (_game_mode != GM_MENU && level != 3) return false;
01032   SetDifficultyLevel(level, &GetGameSettings().difficulty);
01033   return true;
01034 }
01035 
01036 static bool DifficultyChange(int32)
01037 {
01038   if (_game_mode == GM_MENU) {
01039     if (_settings_newgame.difficulty.diff_level != 3) {
01040       ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
01041       _settings_newgame.difficulty.diff_level = 3;
01042     }
01043     SetWindowClassesDirty(WC_SELECT_GAME);
01044   } else {
01045     _settings_game.difficulty.diff_level = 3;
01046   }
01047 
01048   /* If we are a network-client, update the difficult setting (if it is open).
01049    * Use this instead of just dirtying the window because we need to load in
01050    * the new difficulty settings */
01051   if (_networking) InvalidateWindowClassesData(WC_GAME_OPTIONS, GOID_DIFFICULTY_CHANGED);
01052 
01053   return true;
01054 }
01055 
01056 static bool DifficultyNoiseChange(int32 i)
01057 {
01058   if (_game_mode == GM_NORMAL) {
01059     UpdateAirportsNoise();
01060     if (_settings_game.economy.station_noise_level) {
01061       InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01062     }
01063   }
01064 
01065   return DifficultyChange(i);
01066 }
01067 
01068 static bool MaxNoAIsChange(int32 i)
01069 {
01070   if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01071       AI::GetInfoList()->size() == 0 &&
01072       (!_networking || _network_server)) {
01073     ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01074   }
01075 
01076   return DifficultyChange(i);
01077 }
01078 
01084 static bool CheckRoadSide(int p1)
01085 {
01086   extern bool RoadVehiclesAreBuilt();
01087   return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01088 }
01089 
01097 static size_t ConvertLandscape(const char *value)
01098 {
01099   /* try with the old values */
01100   return LookupOneOfMany("normal|hilly|desert|candy", value);
01101 }
01102 
01103 static bool CheckFreeformEdges(int32 p1)
01104 {
01105   if (_game_mode == GM_MENU) return true;
01106   if (p1 != 0) {
01107     Ship *s;
01108     FOR_ALL_SHIPS(s) {
01109       /* Check if there is a ship on the northern border. */
01110       if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01111         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01112         return false;
01113       }
01114     }
01115     BaseStation *st;
01116     FOR_ALL_BASE_STATIONS(st) {
01117       /* Check if there is a non-deleted buoy on the northern border. */
01118       if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01119         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01120         return false;
01121       }
01122     }
01123     for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01124     for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01125   } else {
01126     for (uint i = 0; i < MapMaxX(); i++) {
01127       if (TileHeight(TileXY(i, 1)) != 0) {
01128         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01129         return false;
01130       }
01131     }
01132     for (uint i = 1; i < MapMaxX(); i++) {
01133       if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01134         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01135         return false;
01136       }
01137     }
01138     for (uint i = 0; i < MapMaxY(); i++) {
01139       if (TileHeight(TileXY(1, i)) != 0) {
01140         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01141         return false;
01142       }
01143     }
01144     for (uint i = 1; i < MapMaxY(); i++) {
01145       if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01146         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01147         return false;
01148       }
01149     }
01150     /* Make tiles at the border water again. */
01151     for (uint i = 0; i < MapMaxX(); i++) {
01152       SetTileHeight(TileXY(i, 0), 0);
01153       SetTileType(TileXY(i, 0), MP_WATER);
01154     }
01155     for (uint i = 0; i < MapMaxY(); i++) {
01156       SetTileHeight(TileXY(0, i), 0);
01157       SetTileType(TileXY(0, i), MP_WATER);
01158     }
01159   }
01160   MarkWholeScreenDirty();
01161   return true;
01162 }
01163 
01168 static bool ChangeDynamicEngines(int32 p1)
01169 {
01170   if (_game_mode == GM_MENU) return true;
01171 
01172   if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01173     ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01174     return false;
01175   }
01176 
01177   return true;
01178 }
01179 
01180 static bool StationCatchmentChanged(int32 p1)
01181 {
01182   Station::RecomputeIndustriesNearForAll();
01183   return true;
01184 }
01185 
01186 
01187 #ifdef ENABLE_NETWORK
01188 
01189 static bool UpdateClientName(int32 p1)
01190 {
01191   NetworkUpdateClientName();
01192   return true;
01193 }
01194 
01195 static bool UpdateServerPassword(int32 p1)
01196 {
01197   if (strcmp(_settings_client.network.server_password, "*") == 0) {
01198     _settings_client.network.server_password[0] = '\0';
01199   }
01200 
01201   return true;
01202 }
01203 
01204 static bool UpdateRconPassword(int32 p1)
01205 {
01206   if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01207     _settings_client.network.rcon_password[0] = '\0';
01208   }
01209 
01210   return true;
01211 }
01212 
01213 static bool UpdateClientConfigValues(int32 p1)
01214 {
01215   if (_network_server) NetworkServerSendConfigUpdate();
01216 
01217   return true;
01218 }
01219 
01220 #endif /* ENABLE_NETWORK */
01221 
01222 
01223 /* End - Callback Functions */
01224 
01228 static void PrepareOldDiffCustom()
01229 {
01230   memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01231 }
01232 
01239 static void HandleOldDiffCustom(bool savegame)
01240 {
01241   uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01242 
01243   if (!savegame) {
01244     /* If we did read to old_diff_custom, then at least one value must be non 0. */
01245     bool old_diff_custom_used = false;
01246     for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01247       old_diff_custom_used = (_old_diff_custom[i] != 0);
01248     }
01249 
01250     if (!old_diff_custom_used) return;
01251   }
01252 
01253   for (uint i = 0; i < options_to_load; i++) {
01254     const SettingDesc *sd = &_settings[i];
01255     /* Skip deprecated options */
01256     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01257     void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01258     Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01259   }
01260 }
01261 
01268 static bool ConvertOldNewsSetting(const char *name, const char *value)
01269 {
01270   if (strcasecmp(name, "openclose") == 0) {
01271     /* openclose has been split in "open" and "close".
01272      * So the job is now to decrypt the value of the old news config
01273      * and give it to the two newly introduced ones*/
01274 
01275     NewsDisplay display = ND_OFF; // default
01276     if (strcasecmp(value, "full") == 0) {
01277       display = ND_FULL;
01278     } else if (strcasecmp(value, "summarized") == 0) {
01279       display = ND_SUMMARY;
01280     }
01281     /* tranfert of values */
01282     _news_type_data[NT_INDUSTRY_OPEN].display = display;
01283     _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01284     return true;
01285   }
01286   return false;
01287 }
01288 
01294 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01295 {
01296   IniGroup *group = ini->GetGroup(grpname);
01297   IniItem *item;
01298 
01299   /* If no group exists, return */
01300   if (group == NULL) return;
01301 
01302   for (item = group->item; item != NULL; item = item->next) {
01303     int news_item = -1;
01304     for (int i = 0; i < NT_END; i++) {
01305       if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01306         news_item = i;
01307         break;
01308       }
01309     }
01310 
01311     /* the config been read is not within current aceptable config */
01312     if (news_item == -1) {
01313       /* if the conversion function cannot process it, advice by a debug warning*/
01314       if (!ConvertOldNewsSetting(item->name, item->value)) {
01315         DEBUG(misc, 0, "Invalid display option: %s", item->name);
01316       }
01317       /* in all cases, there is nothing left to do */
01318       continue;
01319     }
01320 
01321     if (StrEmpty(item->value)) {
01322       DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01323       continue;
01324     } else if (strcasecmp(item->value, "full") == 0) {
01325       _news_type_data[news_item].display = ND_FULL;
01326     } else if (strcasecmp(item->value, "off") == 0) {
01327       _news_type_data[news_item].display = ND_OFF;
01328     } else if (strcasecmp(item->value, "summarized") == 0) {
01329       _news_type_data[news_item].display = ND_SUMMARY;
01330     } else {
01331       DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01332       continue;
01333     }
01334   }
01335 }
01336 
01337 static void AILoadConfig(IniFile *ini, const char *grpname)
01338 {
01339   IniGroup *group = ini->GetGroup(grpname);
01340   IniItem *item;
01341 
01342   /* Clean any configured AI */
01343   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01344     AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01345   }
01346 
01347   /* If no group exists, return */
01348   if (group == NULL) return;
01349 
01350   CompanyID c = COMPANY_FIRST;
01351   for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01352     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01353 
01354     config->Change(item->name);
01355     if (!config->HasScript()) {
01356       if (strcmp(item->name, "none") != 0) {
01357         DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01358         continue;
01359       }
01360     }
01361     if (item->value != NULL) config->StringToSettings(item->value);
01362   }
01363 }
01364 
01371 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01372 {
01373   IniGroup *group = ini->GetGroup(grpname);
01374   IniItem *item;
01375   GRFConfig *first = NULL;
01376   GRFConfig **curr = &first;
01377 
01378   if (group == NULL) return NULL;
01379 
01380   for (item = group->item; item != NULL; item = item->next) {
01381     GRFConfig *c = new GRFConfig(item->name);
01382 
01383     /* Parse parameters */
01384     if (!StrEmpty(item->value)) {
01385       c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01386       if (c->num_params == (byte)-1) {
01387         SetDParamStr(0, item->name);
01388         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
01389         c->num_params = 0;
01390       }
01391     }
01392 
01393     /* Check if item is valid */
01394     if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01395       if (c->status == GCS_NOT_FOUND) {
01396         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
01397       } else if (HasBit(c->flags, GCF_UNSAFE)) {
01398         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
01399       } else if (HasBit(c->flags, GCF_SYSTEM)) {
01400         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
01401       } else if (HasBit(c->flags, GCF_INVALID)) {
01402         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
01403       } else {
01404         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
01405       }
01406 
01407       SetDParamStr(0, item->name);
01408       ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
01409       delete c;
01410       continue;
01411     }
01412 
01413     /* Check for duplicate GRFID (will also check for duplicate filenames) */
01414     bool duplicate = false;
01415     for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01416       if (gc->ident.grfid == c->ident.grfid) {
01417         SetDParamStr(0, item->name);
01418         SetDParamStr(1, gc->filename);
01419         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
01420         duplicate = true;
01421         break;
01422       }
01423     }
01424     if (duplicate) {
01425       delete c;
01426       continue;
01427     }
01428 
01429     /* Mark file as static to avoid saving in savegame. */
01430     if (is_static) SetBit(c->flags, GCF_STATIC);
01431 
01432     /* Add item to list */
01433     *curr = c;
01434     curr = &c->next;
01435   }
01436 
01437   return first;
01438 }
01439 
01445 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01446 {
01447   IniGroup *group = ini->GetGroup(grpname);
01448 
01449   for (int i = 0; i < NT_END; i++) {
01450     const char *value;
01451     int v = _news_type_data[i].display;
01452 
01453     value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01454 
01455     group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01456   }
01457 }
01458 
01459 static void AISaveConfig(IniFile *ini, const char *grpname)
01460 {
01461   IniGroup *group = ini->GetGroup(grpname);
01462 
01463   if (group == NULL) return;
01464   group->Clear();
01465 
01466   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01467     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01468     const char *name;
01469     char value[1024];
01470     config->SettingsToString(value, lengthof(value));
01471 
01472     if (config->HasScript()) {
01473       name = config->GetName();
01474     } else {
01475       name = "none";
01476     }
01477 
01478     IniItem *item = new IniItem(group, name, strlen(name));
01479     item->SetValue(value);
01480   }
01481 }
01482 
01487 static void SaveVersionInConfig(IniFile *ini)
01488 {
01489   IniGroup *group = ini->GetGroup("version");
01490 
01491   char version[9];
01492   snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01493 
01494   const char * const versions[][2] = {
01495     { "version_string", _openttd_revision },
01496     { "version_number", version }
01497   };
01498 
01499   for (uint i = 0; i < lengthof(versions); i++) {
01500     group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01501   }
01502 }
01503 
01504 /* Save a GRF configuration to the given group name */
01505 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01506 {
01507   ini->RemoveGroup(grpname);
01508   IniGroup *group = ini->GetGroup(grpname);
01509   const GRFConfig *c;
01510 
01511   for (c = list; c != NULL; c = c->next) {
01512     char params[512];
01513     GRFBuildParamList(params, c, lastof(params));
01514 
01515     group->GetItem(c->filename, true)->SetValue(params);
01516   }
01517 }
01518 
01519 /* Common handler for saving/loading variables to the configuration file */
01520 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool basic_settings = true, bool other_settings = true)
01521 {
01522   if (basic_settings) {
01523     proc(ini, (const SettingDesc*)_misc_settings,    "misc",  NULL);
01524 #if defined(WIN32) && !defined(DEDICATED)
01525     proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
01526 #endif /* WIN32 */
01527   }
01528 
01529   if (other_settings) {
01530     proc(ini, _settings,         "patches",  &_settings_newgame);
01531     proc(ini, _currency_settings,"currency", &_custom_currency);
01532     proc(ini, _company_settings, "company",  &_settings_client.company);
01533 
01534 #ifdef ENABLE_NETWORK
01535     proc_list(ini, "server_bind_addresses", &_network_bind_list);
01536     proc_list(ini, "servers", &_network_host_list);
01537     proc_list(ini, "bans",    &_network_ban_list);
01538 #endif /* ENABLE_NETWORK */
01539   }
01540 }
01541 
01542 static IniFile *IniLoadConfig()
01543 {
01544   IniFile *ini = new IniFile(_list_group_names);
01545   ini->LoadFromDisk(_config_file, BASE_DIR);
01546   return ini;
01547 }
01548 
01553 void LoadFromConfig(bool minimal)
01554 {
01555   IniFile *ini = IniLoadConfig();
01556   if (!minimal) ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
01557 
01558   /* Load basic settings only during bootstrap, load other settings not during bootstrap */
01559   HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList, minimal, !minimal);
01560 
01561   if (!minimal) {
01562     _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01563     _grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
01564     NewsDisplayLoadConfig(ini, "news_display");
01565     AILoadConfig(ini, "ai_players");
01566 
01567     PrepareOldDiffCustom();
01568     IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01569     HandleOldDiffCustom(false);
01570 
01571     ValidateSettings();
01572   }
01573 
01574   delete ini;
01575 }
01576 
01578 void SaveToConfig()
01579 {
01580   IniFile *ini = IniLoadConfig();
01581 
01582   /* Remove some obsolete groups. These have all been loaded into other groups. */
01583   ini->RemoveGroup("patches");
01584   ini->RemoveGroup("yapf");
01585   ini->RemoveGroup("gameopt");
01586 
01587   HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01588   GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01589   GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01590   NewsDisplaySaveConfig(ini, "news_display");
01591   AISaveConfig(ini, "ai_players");
01592   SaveVersionInConfig(ini);
01593   ini->SaveToDisk(_config_file);
01594   delete ini;
01595 }
01596 
01601 void GetGRFPresetList(GRFPresetList *list)
01602 {
01603   list->Clear();
01604 
01605   IniFile *ini = IniLoadConfig();
01606   IniGroup *group;
01607   for (group = ini->group; group != NULL; group = group->next) {
01608     if (strncmp(group->name, "preset-", 7) == 0) {
01609       *list->Append() = strdup(group->name + 7);
01610     }
01611   }
01612 
01613   delete ini;
01614 }
01615 
01622 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01623 {
01624   char *section = (char*)alloca(strlen(config_name) + 8);
01625   sprintf(section, "preset-%s", config_name);
01626 
01627   IniFile *ini = IniLoadConfig();
01628   GRFConfig *config = GRFLoadConfig(ini, section, false);
01629   delete ini;
01630 
01631   return config;
01632 }
01633 
01640 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01641 {
01642   char *section = (char*)alloca(strlen(config_name) + 8);
01643   sprintf(section, "preset-%s", config_name);
01644 
01645   IniFile *ini = IniLoadConfig();
01646   GRFSaveConfig(ini, section, config);
01647   ini->SaveToDisk(_config_file);
01648   delete ini;
01649 }
01650 
01655 void DeleteGRFPresetFromConfig(const char *config_name)
01656 {
01657   char *section = (char*)alloca(strlen(config_name) + 8);
01658   sprintf(section, "preset-%s", config_name);
01659 
01660   IniFile *ini = IniLoadConfig();
01661   ini->RemoveGroup(section);
01662   ini->SaveToDisk(_config_file);
01663   delete ini;
01664 }
01665 
01666 const SettingDesc *GetSettingDescription(uint index)
01667 {
01668   if (index >= lengthof(_settings)) return NULL;
01669   return &_settings[index];
01670 }
01671 
01683 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01684 {
01685   const SettingDesc *sd = GetSettingDescription(p1);
01686 
01687   if (sd == NULL) return CMD_ERROR;
01688   if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01689 
01690   if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01691   if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01692   if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01693       (_game_mode == GM_NORMAL ||
01694       (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01695     return CMD_ERROR;
01696   }
01697 
01698   if (flags & DC_EXEC) {
01699     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01700 
01701     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01702     int32 newval = (int32)p2;
01703 
01704     Write_ValidateSetting(var, sd, newval);
01705     newval = (int32)ReadValue(var, sd->save.conv);
01706 
01707     if (oldval == newval) return CommandCost();
01708 
01709     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01710       WriteValue(var, sd->save.conv, (int64)oldval);
01711       return CommandCost();
01712     }
01713 
01714     if (sd->desc.flags & SGF_NO_NETWORK) {
01715       GamelogStartAction(GLAT_SETTING);
01716       GamelogSetting(sd->desc.name, oldval, newval);
01717       GamelogStopAction();
01718     }
01719 
01720     SetWindowDirty(WC_GAME_OPTIONS, 0);
01721   }
01722 
01723   return CommandCost();
01724 }
01725 
01736 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01737 {
01738   if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01739   const SettingDesc *sd = &_company_settings[p1];
01740 
01741   if (flags & DC_EXEC) {
01742     void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01743 
01744     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01745     int32 newval = (int32)p2;
01746 
01747     Write_ValidateSetting(var, sd, newval);
01748     newval = (int32)ReadValue(var, sd->save.conv);
01749 
01750     if (oldval == newval) return CommandCost();
01751 
01752     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01753       WriteValue(var, sd->save.conv, (int64)oldval);
01754       return CommandCost();
01755     }
01756 
01757     SetWindowDirty(WC_GAME_OPTIONS, 0);
01758   }
01759 
01760   return CommandCost();
01761 }
01762 
01770 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01771 {
01772   const SettingDesc *sd = &_settings[index];
01773   /* If an item is company-based, we do not send it over the network
01774    * (if any) to change. Also *hack*hack* we update the _newgame version
01775    * of settings because changing a company-based setting in a game also
01776    * changes its defaults. At least that is the convention we have chosen */
01777   if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01778     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01779     Write_ValidateSetting(var, sd, value);
01780 
01781     if (_game_mode != GM_MENU) {
01782       void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01783       Write_ValidateSetting(var2, sd, value);
01784     }
01785     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01786     SetWindowDirty(WC_GAME_OPTIONS, 0);
01787     return true;
01788   }
01789 
01790   if (force_newgame) {
01791     void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01792     Write_ValidateSetting(var2, sd, value);
01793     return true;
01794   }
01795 
01796   /* send non-company-based settings over the network */
01797   if (!_networking || (_networking && _network_server)) {
01798     return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01799   }
01800   return false;
01801 }
01802 
01809 void SetCompanySetting(uint index, int32 value)
01810 {
01811   const SettingDesc *sd = &_company_settings[index];
01812   if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01813     DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01814   } else {
01815     void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01816     Write_ValidateSetting(var, sd, value);
01817     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01818   }
01819 }
01820 
01824 void SetDefaultCompanySettings(CompanyID cid)
01825 {
01826   Company *c = Company::Get(cid);
01827   const SettingDesc *sd;
01828   for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01829     void *var = GetVariableAddress(&c->settings, &sd->save);
01830     Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01831   }
01832 }
01833 
01834 #if defined(ENABLE_NETWORK)
01835 
01838 void SyncCompanySettings()
01839 {
01840   const SettingDesc *sd;
01841   uint i = 0;
01842   for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01843     const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01844     const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01845     uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01846     uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01847     if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01848   }
01849 }
01850 #endif /* ENABLE_NETWORK */
01851 
01857 uint GetCompanySettingIndex(const char *name)
01858 {
01859   uint i;
01860   const SettingDesc *sd = GetSettingFromName(name, &i);
01861   assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01862   return i;
01863 }
01864 
01872 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01873 {
01874   const SettingDesc *sd = &_settings[index];
01875   assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
01876 
01877   if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01878     char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01879     free(*var);
01880     *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01881   } else {
01882     char *var = (char*)GetVariableAddress(NULL, &sd->save);
01883     ttd_strlcpy(var, value, sd->save.length);
01884   }
01885   if (sd->desc.proc != NULL) sd->desc.proc(0);
01886 
01887   return true;
01888 }
01889 
01897 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01898 {
01899   const SettingDesc *sd;
01900 
01901   /* First check all full names */
01902   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01903     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01904     if (strcmp(sd->desc.name, name) == 0) return sd;
01905   }
01906 
01907   /* Then check the shortcut variant of the name. */
01908   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01909     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01910     const char *short_name = strchr(sd->desc.name, '.');
01911     if (short_name != NULL) {
01912       short_name++;
01913       if (strcmp(short_name, name) == 0) return sd;
01914     }
01915   }
01916 
01917   if (strncmp(name, "company.", 8) == 0) name += 8;
01918   /* And finally the company-based settings */
01919   for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01920     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01921     if (strcmp(sd->desc.name, name) == 0) return sd;
01922   }
01923 
01924   return NULL;
01925 }
01926 
01927 /* Those 2 functions need to be here, else we have to make some stuff non-static
01928  * and besides, it is also better to keep stuff like this at the same place */
01929 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01930 {
01931   uint index;
01932   const SettingDesc *sd = GetSettingFromName(name, &index);
01933 
01934   if (sd == NULL) {
01935     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01936     return;
01937   }
01938 
01939   bool success;
01940   if (sd->desc.cmd == SDT_STRING) {
01941     success = SetSettingValue(index, value, force_newgame);
01942   } else {
01943     uint32 val;
01944     extern bool GetArgumentInteger(uint32 *value, const char *arg);
01945     success = GetArgumentInteger(&val, value);
01946     if (!success) {
01947       IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01948       return;
01949     }
01950 
01951     success = SetSettingValue(index, val, force_newgame);
01952   }
01953 
01954   if (!success) {
01955     if (_network_server) {
01956       IConsoleError("This command/variable is not available during network games.");
01957     } else {
01958       IConsoleError("This command/variable is only available to a network server.");
01959     }
01960   }
01961 }
01962 
01963 void IConsoleSetSetting(const char *name, int value)
01964 {
01965   uint index;
01966   const SettingDesc *sd = GetSettingFromName(name, &index);
01967   assert(sd != NULL);
01968   SetSettingValue(index, value);
01969 }
01970 
01976 void IConsoleGetSetting(const char *name, bool force_newgame)
01977 {
01978   char value[20];
01979   uint index;
01980   const SettingDesc *sd = GetSettingFromName(name, &index);
01981   const void *ptr;
01982 
01983   if (sd == NULL) {
01984     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01985     return;
01986   }
01987 
01988   ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01989 
01990   if (sd->desc.cmd == SDT_STRING) {
01991     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
01992   } else {
01993     if (sd->desc.cmd == SDT_BOOLX) {
01994       snprintf(value, sizeof(value), (*(const bool*)ptr != 0) ? "on" : "off");
01995     } else {
01996       snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01997     }
01998 
01999     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
02000       name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
02001   }
02002 }
02003 
02009 void IConsoleListSettings(const char *prefilter)
02010 {
02011   IConsolePrintF(CC_WARNING, "All settings with their current value:");
02012 
02013   for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
02014     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02015     if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
02016     char value[80];
02017     const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
02018 
02019     if (sd->desc.cmd == SDT_BOOLX) {
02020       snprintf(value, lengthof(value), (*(const bool *)ptr != 0) ? "on" : "off");
02021     } else if (sd->desc.cmd == SDT_STRING) {
02022       snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02023     } else {
02024       snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02025     }
02026     IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
02027   }
02028 
02029   IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
02030 }
02031 
02038 static void LoadSettings(const SettingDesc *osd, void *object)
02039 {
02040   for (; osd->save.cmd != SL_END; osd++) {
02041     const SaveLoad *sld = &osd->save;
02042     void *ptr = GetVariableAddress(object, sld);
02043 
02044     if (!SlObjectMember(ptr, sld)) continue;
02045     if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02046   }
02047 }
02048 
02055 static void SaveSettings(const SettingDesc *sd, void *object)
02056 {
02057   /* We need to write the CH_RIFF header, but unfortunately can't call
02058    * SlCalcLength() because we have a different format. So do this manually */
02059   const SettingDesc *i;
02060   size_t length = 0;
02061   for (i = sd; i->save.cmd != SL_END; i++) {
02062     length += SlCalcObjMemberLength(object, &i->save);
02063   }
02064   SlSetLength(length);
02065 
02066   for (i = sd; i->save.cmd != SL_END; i++) {
02067     void *ptr = GetVariableAddress(object, &i->save);
02068     SlObjectMember(ptr, &i->save);
02069   }
02070 }
02071 
02072 static void Load_OPTS()
02073 {
02074   /* Copy over default setting since some might not get loaded in
02075    * a networking environment. This ensures for example that the local
02076    * autosave-frequency stays when joining a network-server */
02077   PrepareOldDiffCustom();
02078   LoadSettings(_gameopt_settings, &_settings_game);
02079   HandleOldDiffCustom(true);
02080 }
02081 
02082 static void Load_PATS()
02083 {
02084   /* Copy over default setting since some might not get loaded in
02085    * a networking environment. This ensures for example that the local
02086    * signal_side stays when joining a network-server */
02087   LoadSettings(_settings, &_settings_game);
02088 }
02089 
02090 static void Check_PATS()
02091 {
02092   LoadSettings(_settings, &_load_check_data.settings);
02093 }
02094 
02095 static void Save_PATS()
02096 {
02097   SaveSettings(_settings, &_settings_game);
02098 }
02099 
02100 void CheckConfig()
02101 {
02102   /*
02103    * Increase old default values for pf_maxdepth and pf_maxlength
02104    * to support big networks.
02105    */
02106   if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02107     _settings_newgame.pf.opf.pf_maxdepth = 48;
02108     _settings_newgame.pf.opf.pf_maxlength = 4096;
02109   }
02110 }
02111 
02112 extern const ChunkHandler _setting_chunk_handlers[] = {
02113   { 'OPTS', NULL,      Load_OPTS, NULL, NULL,       CH_RIFF},
02114   { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02115 };
02116 
02117 static bool IsSignedVarMemType(VarType vt)
02118 {
02119   switch (GetVarMemType(vt)) {
02120     case SLE_VAR_I8:
02121     case SLE_VAR_I16:
02122     case SLE_VAR_I32:
02123     case SLE_VAR_I64:
02124       return true;
02125   }
02126   return false;
02127 }