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 "game/game_config.hpp"
00061 #include "game/game.hpp"
00062 #include "ship.h"
00063 #include "smallmap_gui.h"
00064 #include "roadveh.h"
00065 #include "fios.h"
00066 #include "strings_func.h"
00067 
00068 #include "void_map.h"
00069 #include "station_base.h"
00070 
00071 #include "table/strings.h"
00072 #include "table/settings.h"
00073 
00074 ClientSettings _settings_client;
00075 GameSettings _settings_game;     
00076 GameSettings _settings_newgame;  
00077 VehicleDefaultSettings _old_vds; 
00078 char *_config_file; 
00079 
00080 typedef std::list<ErrorMessageData> ErrorList;
00081 static ErrorList _settings_error_list; 
00082 
00083 
00084 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00085 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00086 
00087 static bool IsSignedVarMemType(VarType vt);
00088 
00092 static const char * const _list_group_names[] = {
00093   "bans",
00094   "newgrf",
00095   "servers",
00096   "server_bind_addresses",
00097   NULL
00098 };
00099 
00107 static size_t LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00108 {
00109   const char *s;
00110   size_t idx;
00111 
00112   if (onelen == 0) onelen = strlen(one);
00113 
00114   /* check if it's an integer */
00115   if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00116 
00117   idx = 0;
00118   for (;;) {
00119     /* find end of item */
00120     s = many;
00121     while (*s != '|' && *s != 0) s++;
00122     if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00123     if (*s == 0) return (size_t)-1;
00124     many = s + 1;
00125     idx++;
00126   }
00127 }
00128 
00136 static size_t LookupManyOfMany(const char *many, const char *str)
00137 {
00138   const char *s;
00139   size_t r;
00140   size_t res = 0;
00141 
00142   for (;;) {
00143     /* skip "whitespace" */
00144     while (*str == ' ' || *str == '\t' || *str == '|') str++;
00145     if (*str == 0) break;
00146 
00147     s = str;
00148     while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00149 
00150     r = LookupOneOfMany(many, str, s - str);
00151     if (r == (size_t)-1) return r;
00152 
00153     SetBit(res, (uint8)r); // value found, set it
00154     if (*s == 0) break;
00155     str = s + 1;
00156   }
00157   return res;
00158 }
00159 
00168 static int ParseIntList(const char *p, int *items, int maxitems)
00169 {
00170   int n = 0; // number of items read so far
00171   bool comma = false; // do we accept comma?
00172 
00173   while (*p != '\0') {
00174     switch (*p) {
00175       case ',':
00176         /* Do not accept multiple commas between numbers */
00177         if (!comma) return -1;
00178         comma = false;
00179         /* FALL THROUGH */
00180       case ' ':
00181         p++;
00182         break;
00183 
00184       default: {
00185         if (n == maxitems) return -1; // we don't accept that many numbers
00186         char *end;
00187         long v = strtol(p, &end, 0);
00188         if (p == end) return -1; // invalid character (not a number)
00189         if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00190         items[n++] = v;
00191         p = end; // first non-number
00192         comma = true; // we accept comma now
00193         break;
00194       }
00195     }
00196   }
00197 
00198   /* If we have read comma but no number after it, fail.
00199    * We have read comma when (n != 0) and comma is not allowed */
00200   if (n != 0 && !comma) return -1;
00201 
00202   return n;
00203 }
00204 
00213 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00214 {
00215   int items[64];
00216   int i, nitems;
00217 
00218   if (str == NULL) {
00219     memset(items, 0, sizeof(items));
00220     nitems = nelems;
00221   } else {
00222     nitems = ParseIntList(str, items, lengthof(items));
00223     if (nitems != nelems) return false;
00224   }
00225 
00226   switch (type) {
00227     case SLE_VAR_BL:
00228     case SLE_VAR_I8:
00229     case SLE_VAR_U8:
00230       for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00231       break;
00232 
00233     case SLE_VAR_I16:
00234     case SLE_VAR_U16:
00235       for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00236       break;
00237 
00238     case SLE_VAR_I32:
00239     case SLE_VAR_U32:
00240       for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00241       break;
00242 
00243     default: NOT_REACHED();
00244   }
00245 
00246   return true;
00247 }
00248 
00258 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00259 {
00260   int i, v = 0;
00261   const byte *p = (const byte *)array;
00262 
00263   for (i = 0; i != nelems; i++) {
00264     switch (type) {
00265       case SLE_VAR_BL:
00266       case SLE_VAR_I8:  v = *(const   int8 *)p; p += 1; break;
00267       case SLE_VAR_U8:  v = *(const  uint8 *)p; p += 1; break;
00268       case SLE_VAR_I16: v = *(const  int16 *)p; p += 2; break;
00269       case SLE_VAR_U16: v = *(const uint16 *)p; p += 2; break;
00270       case SLE_VAR_I32: v = *(const  int32 *)p; p += 4; break;
00271       case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
00272       default: NOT_REACHED();
00273     }
00274     buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00275   }
00276 }
00277 
00285 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00286 {
00287   int orig_id = id;
00288 
00289   /* Look for the id'th element */
00290   while (--id >= 0) {
00291     for (; *many != '|'; many++) {
00292       if (*many == '\0') { // not found
00293         seprintf(buf, last, "%d", orig_id);
00294         return;
00295       }
00296     }
00297     many++; // pass the |-character
00298   }
00299 
00300   /* copy string until next item (|) or the end of the list if this is the last one */
00301   while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00302   *buf = '\0';
00303 }
00304 
00313 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00314 {
00315   const char *start;
00316   int i = 0;
00317   bool init = true;
00318 
00319   for (; x != 0; x >>= 1, i++) {
00320     start = many;
00321     while (*many != 0 && *many != '|') many++; // advance to the next element
00322 
00323     if (HasBit(x, 0)) { // item found, copy it
00324       if (!init) buf += seprintf(buf, last, "|");
00325       init = false;
00326       if (start == many) {
00327         buf += seprintf(buf, last, "%d", i);
00328       } else {
00329         memcpy(buf, start, many - start);
00330         buf += many - start;
00331       }
00332     }
00333 
00334     if (*many == '|') many++;
00335   }
00336 
00337   *buf = '\0';
00338 }
00339 
00346 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00347 {
00348   const char *str = orig_str == NULL ? "" : orig_str;
00349 
00350   switch (desc->cmd) {
00351     case SDT_NUMX: {
00352       char *end;
00353       size_t val = strtoul(str, &end, 0);
00354       if (end == str) {
00355         ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00356         msg.SetDParamStr(0, str);
00357         msg.SetDParamStr(1, desc->name);
00358         _settings_error_list.push_back(msg);
00359         return desc->def;
00360       }
00361       if (*end != '\0') {
00362         ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_TRAILING_CHARACTERS);
00363         msg.SetDParamStr(0, desc->name);
00364         _settings_error_list.push_back(msg);
00365       }
00366       return (void*)val;
00367     }
00368 
00369     case SDT_ONEOFMANY: {
00370       size_t r = LookupOneOfMany(desc->many, str);
00371       /* if the first attempt of conversion from string to the appropriate value fails,
00372        * look if we have defined a converter from old value to new value. */
00373       if (r == (size_t)-1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00374       if (r != (size_t)-1) return (void*)r; // and here goes converted value
00375 
00376       ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00377       msg.SetDParamStr(0, str);
00378       msg.SetDParamStr(1, desc->name);
00379       _settings_error_list.push_back(msg);
00380       return desc->def;
00381     }
00382 
00383     case SDT_MANYOFMANY: {
00384       size_t r = LookupManyOfMany(desc->many, str);
00385       if (r != (size_t)-1) return (void*)r;
00386       ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00387       msg.SetDParamStr(0, str);
00388       msg.SetDParamStr(1, desc->name);
00389       _settings_error_list.push_back(msg);
00390       return desc->def;
00391     }
00392 
00393     case SDT_BOOLX: {
00394       if (strcmp(str, "true")  == 0 || strcmp(str, "on")  == 0 || strcmp(str, "1") == 0) return (void*)true;
00395       if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00396 
00397       ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
00398       msg.SetDParamStr(0, str);
00399       msg.SetDParamStr(1, desc->name);
00400       _settings_error_list.push_back(msg);
00401       return desc->def;
00402     }
00403 
00404     case SDT_STRING: return orig_str;
00405     case SDT_INTLIST: return str;
00406     default: break;
00407   }
00408 
00409   return NULL;
00410 }
00411 
00421 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00422 {
00423   const SettingDescBase *sdb = &sd->desc;
00424 
00425   if (sdb->cmd != SDT_BOOLX &&
00426       sdb->cmd != SDT_NUMX &&
00427       sdb->cmd != SDT_ONEOFMANY &&
00428       sdb->cmd != SDT_MANYOFMANY) {
00429     return;
00430   }
00431 
00432   /* We cannot know the maximum value of a bitset variable, so just have faith */
00433   if (sdb->cmd != SDT_MANYOFMANY) {
00434     /* We need to take special care of the uint32 type as we receive from the function
00435      * a signed integer. While here also bail out on 64-bit settings as those are not
00436      * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
00437      * 32-bit variable
00438      * TODO: Support 64-bit settings/variables */
00439     switch (GetVarMemType(sd->save.conv)) {
00440       case SLE_VAR_NULL: return;
00441       case SLE_VAR_BL:
00442       case SLE_VAR_I8:
00443       case SLE_VAR_U8:
00444       case SLE_VAR_I16:
00445       case SLE_VAR_U16:
00446       case SLE_VAR_I32: {
00447         /* Override the minimum value. No value below sdb->min, except special value 0 */
00448         if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00449         break;
00450       }
00451       case SLE_VAR_U32: {
00452         /* Override the minimum value. No value below sdb->min, except special value 0 */
00453         uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00454         WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00455         return;
00456       }
00457       case SLE_VAR_I64:
00458       case SLE_VAR_U64:
00459       default: NOT_REACHED();
00460     }
00461   }
00462 
00463   WriteValue(ptr, sd->save.conv, (int64)val);
00464 }
00465 
00474 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00475 {
00476   IniGroup *group;
00477   IniGroup *group_def = ini->GetGroup(grpname);
00478   IniItem *item;
00479   const void *p;
00480   void *ptr;
00481   const char *s;
00482 
00483   for (; sd->save.cmd != SL_END; sd++) {
00484     const SettingDescBase *sdb = &sd->desc;
00485     const SaveLoad        *sld = &sd->save;
00486 
00487     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00488 
00489     /* For settings.xx.yy load the settings from [xx] yy = ? */
00490     s = strchr(sdb->name, '.');
00491     if (s != NULL) {
00492       group = ini->GetGroup(sdb->name, s - sdb->name);
00493       s++;
00494     } else {
00495       s = sdb->name;
00496       group = group_def;
00497     }
00498 
00499     item = group->GetItem(s, false);
00500     if (item == NULL && group != group_def) {
00501       /* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
00502        * did not exist (e.g. loading old config files with a [settings] section */
00503       item = group_def->GetItem(s, false);
00504     }
00505     if (item == NULL) {
00506       /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
00507        * did not exist (e.g. loading old config files with a [yapf] section */
00508       const char *sc = strchr(s, '.');
00509       if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00510     }
00511 
00512     p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00513     ptr = GetVariableAddress(object, sld);
00514 
00515     switch (sdb->cmd) {
00516       case SDT_BOOLX: // All four are various types of (integer) numbers
00517       case SDT_NUMX:
00518       case SDT_ONEOFMANY:
00519       case SDT_MANYOFMANY:
00520         Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
00521         break;
00522 
00523       case SDT_STRING:
00524         switch (GetVarMemType(sld->conv)) {
00525           case SLE_VAR_STRB:
00526           case SLE_VAR_STRBQ:
00527             if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00528             break;
00529 
00530           case SLE_VAR_STR:
00531           case SLE_VAR_STRQ:
00532             free(*(char**)ptr);
00533             *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00534             break;
00535 
00536           case SLE_VAR_CHAR: if (p != NULL) *(char *)ptr = *(const char *)p; break;
00537 
00538           default: NOT_REACHED();
00539         }
00540         break;
00541 
00542       case SDT_INTLIST: {
00543         if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00544           ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
00545           msg.SetDParamStr(0, sdb->name);
00546           _settings_error_list.push_back(msg);
00547 
00548           /* Use default */
00549           LoadIntList((const char*)sdb->def, ptr, sld->length, GetVarMemType(sld->conv));
00550         } else if (sd->desc.proc_cnvt != NULL) {
00551           sd->desc.proc_cnvt((const char*)p);
00552         }
00553         break;
00554       }
00555       default: NOT_REACHED();
00556     }
00557   }
00558 }
00559 
00572 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00573 {
00574   IniGroup *group_def = NULL, *group;
00575   IniItem *item;
00576   char buf[512];
00577   const char *s;
00578   void *ptr;
00579 
00580   for (; sd->save.cmd != SL_END; sd++) {
00581     const SettingDescBase *sdb = &sd->desc;
00582     const SaveLoad        *sld = &sd->save;
00583 
00584     /* If the setting is not saved to the configuration
00585      * file, just continue with the next setting */
00586     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00587     if (sld->conv & SLF_NOT_IN_CONFIG) continue;
00588 
00589     /* XXX - wtf is this?? (group override?) */
00590     s = strchr(sdb->name, '.');
00591     if (s != NULL) {
00592       group = ini->GetGroup(sdb->name, s - sdb->name);
00593       s++;
00594     } else {
00595       if (group_def == NULL) group_def = ini->GetGroup(grpname);
00596       s = sdb->name;
00597       group = group_def;
00598     }
00599 
00600     item = group->GetItem(s, true);
00601     ptr = GetVariableAddress(object, sld);
00602 
00603     if (item->value != NULL) {
00604       /* check if the value is the same as the old value */
00605       const void *p = StringToVal(sdb, item->value);
00606 
00607       /* The main type of a variable/setting is in bytes 8-15
00608        * The subtype (what kind of numbers do we have there) is in 0-7 */
00609       switch (sdb->cmd) {
00610         case SDT_BOOLX:
00611         case SDT_NUMX:
00612         case SDT_ONEOFMANY:
00613         case SDT_MANYOFMANY:
00614           switch (GetVarMemType(sld->conv)) {
00615             case SLE_VAR_BL:
00616               if (*(bool*)ptr == (p != NULL)) continue;
00617               break;
00618 
00619             case SLE_VAR_I8:
00620             case SLE_VAR_U8:
00621               if (*(byte*)ptr == (byte)(size_t)p) continue;
00622               break;
00623 
00624             case SLE_VAR_I16:
00625             case SLE_VAR_U16:
00626               if (*(uint16*)ptr == (uint16)(size_t)p) continue;
00627               break;
00628 
00629             case SLE_VAR_I32:
00630             case SLE_VAR_U32:
00631               if (*(uint32*)ptr == (uint32)(size_t)p) continue;
00632               break;
00633 
00634             default: NOT_REACHED();
00635           }
00636           break;
00637 
00638         default: break; // Assume the other types are always changed
00639       }
00640     }
00641 
00642     /* Value has changed, get the new value and put it into a buffer */
00643     switch (sdb->cmd) {
00644       case SDT_BOOLX:
00645       case SDT_NUMX:
00646       case SDT_ONEOFMANY:
00647       case SDT_MANYOFMANY: {
00648         uint32 i = (uint32)ReadValue(ptr, sld->conv);
00649 
00650         switch (sdb->cmd) {
00651           case SDT_BOOLX:      strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00652           case SDT_NUMX:       seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00653           case SDT_ONEOFMANY:  MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00654           case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00655           default: NOT_REACHED();
00656         }
00657         break;
00658       }
00659 
00660       case SDT_STRING:
00661         switch (GetVarMemType(sld->conv)) {
00662           case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00663           case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00664           case SLE_VAR_STR:  strecpy(buf, *(char**)ptr, lastof(buf)); break;
00665 
00666           case SLE_VAR_STRQ:
00667             if (*(char**)ptr == NULL) {
00668               buf[0] = '\0';
00669             } else {
00670               seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00671             }
00672             break;
00673 
00674           case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00675           default: NOT_REACHED();
00676         }
00677         break;
00678 
00679       case SDT_INTLIST:
00680         MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00681         break;
00682 
00683       default: NOT_REACHED();
00684     }
00685 
00686     /* The value is different, that means we have to write it to the ini */
00687     free(item->value);
00688     item->value = strdup(buf);
00689   }
00690 }
00691 
00701 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00702 {
00703   IniGroup *group = ini->GetGroup(grpname);
00704 
00705   if (group == NULL || list == NULL) return;
00706 
00707   list->Clear();
00708 
00709   for (const IniItem *item = group->item; item != NULL; item = item->next) {
00710     if (item->name != NULL) *list->Append() = strdup(item->name);
00711   }
00712 }
00713 
00723 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00724 {
00725   IniGroup *group = ini->GetGroup(grpname);
00726 
00727   if (group == NULL || list == NULL) return;
00728   group->Clear();
00729 
00730   for (char **iter = list->Begin(); iter != list->End(); iter++) {
00731     group->GetItem(*iter, true)->SetValue("");
00732   }
00733 }
00734 
00735 /* Begin - Callback Functions for the various settings. */
00736 
00738 static bool v_PositionMainToolbar(int32 p1)
00739 {
00740   if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00741   return true;
00742 }
00743 
00745 static bool v_PositionStatusbar(int32 p1)
00746 {
00747   if (_game_mode != GM_MENU) {
00748     PositionStatusbar(NULL);
00749     PositionNewsMessage(NULL);
00750     PositionNetworkChatWindow(NULL);
00751   }
00752   return true;
00753 }
00754 
00755 static bool PopulationInLabelActive(int32 p1)
00756 {
00757   UpdateAllTownVirtCoords();
00758   return true;
00759 }
00760 
00761 static bool RedrawScreen(int32 p1)
00762 {
00763   MarkWholeScreenDirty();
00764   return true;
00765 }
00766 
00772 static bool RedrawSmallmap(int32 p1)
00773 {
00774   BuildLandLegend();
00775   BuildOwnerLegend();
00776   SetWindowClassesDirty(WC_SMALLMAP);
00777   return true;
00778 }
00779 
00780 static bool InvalidateDetailsWindow(int32 p1)
00781 {
00782   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00783   return true;
00784 }
00785 
00786 static bool InvalidateStationBuildWindow(int32 p1)
00787 {
00788   SetWindowDirty(WC_BUILD_STATION, 0);
00789   return true;
00790 }
00791 
00792 static bool InvalidateBuildIndustryWindow(int32 p1)
00793 {
00794   InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00795   return true;
00796 }
00797 
00798 static bool CloseSignalGUI(int32 p1)
00799 {
00800   if (p1 == 0) {
00801     DeleteWindowByClass(WC_BUILD_SIGNAL);
00802   }
00803   return true;
00804 }
00805 
00806 static bool InvalidateTownViewWindow(int32 p1)
00807 {
00808   InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00809   return true;
00810 }
00811 
00812 static bool DeleteSelectStationWindow(int32 p1)
00813 {
00814   DeleteWindowById(WC_SELECT_STATION, 0);
00815   return true;
00816 }
00817 
00818 static bool UpdateConsists(int32 p1)
00819 {
00820   Train *t;
00821   FOR_ALL_TRAINS(t) {
00822     /* Update the consist of all trains so the maximum speed is set correctly. */
00823     if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00824   }
00825   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00826   return true;
00827 }
00828 
00829 /* Check service intervals of vehicles, p1 is value of % or day based servicing */
00830 static bool CheckInterval(int32 p1)
00831 {
00832   VehicleDefaultSettings *vds;
00833   if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00834     vds = &_settings_client.company.vehicle;
00835   } else {
00836     vds = &Company::Get(_current_company)->settings.vehicle;
00837   }
00838 
00839   if (p1 != 0) {
00840     vds->servint_trains   = 50;
00841     vds->servint_roadveh  = 50;
00842     vds->servint_aircraft = 50;
00843     vds->servint_ships    = 50;
00844   } else {
00845     vds->servint_trains   = 150;
00846     vds->servint_roadveh  = 150;
00847     vds->servint_aircraft = 100;
00848     vds->servint_ships    = 360;
00849   }
00850 
00851   InvalidateDetailsWindow(0);
00852 
00853   return true;
00854 }
00855 
00856 static bool TrainAccelerationModelChanged(int32 p1)
00857 {
00858   Train *t;
00859   FOR_ALL_TRAINS(t) {
00860     if (t->IsFrontEngine()) {
00861       t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00862       t->UpdateAcceleration();
00863     }
00864   }
00865 
00866   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00867   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00868   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00869   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00870 
00871   return true;
00872 }
00873 
00879 static bool TrainSlopeSteepnessChanged(int32 p1)
00880 {
00881   Train *t;
00882   FOR_ALL_TRAINS(t) {
00883     if (t->IsFrontEngine()) t->CargoChanged();
00884   }
00885 
00886   return true;
00887 }
00888 
00894 static bool RoadVehAccelerationModelChanged(int32 p1)
00895 {
00896   if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00897     RoadVehicle *rv;
00898     FOR_ALL_ROADVEHICLES(rv) {
00899       if (rv->IsFrontEngine()) {
00900         rv->CargoChanged();
00901       }
00902     }
00903   }
00904 
00905   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00906   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00907   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00908   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00909 
00910   return true;
00911 }
00912 
00918 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00919 {
00920   RoadVehicle *rv;
00921   FOR_ALL_ROADVEHICLES(rv) {
00922     if (rv->IsFrontEngine()) rv->CargoChanged();
00923   }
00924 
00925   return true;
00926 }
00927 
00928 static bool DragSignalsDensityChanged(int32)
00929 {
00930   InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00931 
00932   return true;
00933 }
00934 
00935 static bool TownFoundingChanged(int32 p1)
00936 {
00937   if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00938     DeleteWindowById(WC_FOUND_TOWN, 0);
00939     return true;
00940   }
00941   InvalidateWindowData(WC_FOUND_TOWN, 0);
00942   return true;
00943 }
00944 
00945 static bool InvalidateVehTimetableWindow(int32 p1)
00946 {
00947   InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00948   return true;
00949 }
00950 
00951 static bool ZoomMinMaxChanged(int32 p1)
00952 {
00953   extern void ConstrainAllViewportsZoom();
00954   ConstrainAllViewportsZoom();
00955   GfxClearSpriteCache();
00956   return true;
00957 }
00958 
00966 static bool InvalidateNewGRFChangeWindows(int32 p1)
00967 {
00968   InvalidateWindowClassesData(WC_SAVELOAD);
00969   DeleteWindowByClass(WC_GAME_OPTIONS);
00970   ReInitAllWindows();
00971   return true;
00972 }
00973 
00974 static bool InvalidateCompanyLiveryWindow(int32 p1)
00975 {
00976   InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00977   return RedrawScreen(p1);
00978 }
00979 
00980 static bool InvalidateIndustryViewWindow(int32 p1)
00981 {
00982   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00983   return true;
00984 }
00985 
00986 static bool InvalidateAISettingsWindow(int32 p1)
00987 {
00988   InvalidateWindowClassesData(WC_AI_SETTINGS);
00989   return true;
00990 }
00991 
00997 static bool RedrawTownAuthority(int32 p1)
00998 {
00999   SetWindowClassesDirty(WC_TOWN_AUTHORITY);
01000   return true;
01001 }
01002 
01008 static bool InvalidateCompanyInfrastructureWindow(int32 p1)
01009 {
01010   InvalidateWindowClassesData(WC_COMPANY_INFRASTRUCTURE);
01011   return true;
01012 }
01013 
01014 /*
01015  * A: competitors
01016  * B: competitor start time. Deprecated since savegame version 110.
01017  * C: town count (3 = high, 0 = very low)
01018  * D: industry count (4 = high, 0 = none)
01019  * E: inital loan (in GBP)
01020  * F: interest rate
01021  * G: running costs (0 = low, 2 = high)
01022  * H: construction speed of competitors (0 = very slow, 4 = very fast)
01023  * I: competitor intelligence. Deprecated since savegame version 110.
01024  * J: breakdowns (0 = off, 2 = normal)
01025  * K: subsidy multiplier (0 = 1.5, 3 = 4.0)
01026  * L: construction cost (0-2)
01027  * M: terrain type (0 = very flat, 3 = mountainous)
01028  * N: amount of water (0 = very low, 3 = high)
01029  * O: economy (0 = steady, 1 = fluctuating)
01030  * P: Train reversing (0 = end of line + stations, 1 = end of line)
01031  * Q: disasters
01032  * R: area restructuring (0 = permissive, 2 = hostile)
01033  * S: the difficulty level
01034  */
01035 static const DifficultySettings _default_game_diff[3] = { /*
01036    A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
01037   {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0}, 
01038   {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1}, 
01039   {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2}, 
01040 };
01041 
01042 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
01043 {
01044   assert(mode <= 3);
01045 
01046   if (mode != 3) {
01047     *gm_opt = _default_game_diff[mode];
01048   } else {
01049     gm_opt->diff_level = 3;
01050   }
01051 }
01052 
01054 static void ValidateSettings()
01055 {
01056   /* Force the difficulty levels to correct values if they are invalid. */
01057   if (_settings_newgame.difficulty.diff_level != 3) {
01058     SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
01059   }
01060 
01061   /* Do not allow a custom sea level with the original land generator. */
01062   if (_settings_newgame.game_creation.land_generator == 0 &&
01063       _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
01064     _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
01065   }
01066 }
01067 
01068 static bool DifficultyReset(int32 level)
01069 {
01070   /* In game / in the scenario editor you can set the difficulty level only to custom. This is
01071    * needed by the AI Gui code that sets the difficulty level when you change any AI settings. */
01072   if (_game_mode != GM_MENU && level != 3) return false;
01073   SetDifficultyLevel(level, &GetGameSettings().difficulty);
01074   return true;
01075 }
01076 
01077 static bool DifficultyChange(int32)
01078 {
01079   if (_game_mode == GM_MENU) {
01080     if (_settings_newgame.difficulty.diff_level != 3) {
01081       ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
01082       _settings_newgame.difficulty.diff_level = 3;
01083     }
01084     SetWindowClassesDirty(WC_SELECT_GAME);
01085   } else {
01086     _settings_game.difficulty.diff_level = 3;
01087   }
01088 
01089   /* If we are a network-client, update the difficult setting (if it is open).
01090    * Use this instead of just dirtying the window because we need to load in
01091    * the new difficulty settings */
01092   if (_networking) InvalidateWindowClassesData(WC_GAME_OPTIONS, GOID_DIFFICULTY_CHANGED);
01093 
01094   return true;
01095 }
01096 
01097 static bool DifficultyNoiseChange(int32 i)
01098 {
01099   if (_game_mode == GM_NORMAL) {
01100     UpdateAirportsNoise();
01101     if (_settings_game.economy.station_noise_level) {
01102       InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01103     }
01104   }
01105 
01106   return DifficultyChange(i);
01107 }
01108 
01109 static bool MaxNoAIsChange(int32 i)
01110 {
01111   if (GetGameSettings().difficulty.max_no_competitors != 0 &&
01112       AI::GetInfoList()->size() == 0 &&
01113       (!_networking || _network_server)) {
01114     ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01115   }
01116 
01117   return DifficultyChange(i);
01118 }
01119 
01125 static bool CheckRoadSide(int p1)
01126 {
01127   extern bool RoadVehiclesAreBuilt();
01128   return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01129 }
01130 
01138 static size_t ConvertLandscape(const char *value)
01139 {
01140   /* try with the old values */
01141   return LookupOneOfMany("normal|hilly|desert|candy", value);
01142 }
01143 
01144 static bool CheckFreeformEdges(int32 p1)
01145 {
01146   if (_game_mode == GM_MENU) return true;
01147   if (p1 != 0) {
01148     Ship *s;
01149     FOR_ALL_SHIPS(s) {
01150       /* Check if there is a ship on the northern border. */
01151       if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01152         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01153         return false;
01154       }
01155     }
01156     BaseStation *st;
01157     FOR_ALL_BASE_STATIONS(st) {
01158       /* Check if there is a non-deleted buoy on the northern border. */
01159       if (st->IsInUse() && (TileX(st->xy) == 0 || TileY(st->xy) == 0)) {
01160         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01161         return false;
01162       }
01163     }
01164     for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01165     for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01166   } else {
01167     for (uint i = 0; i < MapMaxX(); i++) {
01168       if (TileHeight(TileXY(i, 1)) != 0) {
01169         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01170         return false;
01171       }
01172     }
01173     for (uint i = 1; i < MapMaxX(); i++) {
01174       if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01175         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01176         return false;
01177       }
01178     }
01179     for (uint i = 0; i < MapMaxY(); i++) {
01180       if (TileHeight(TileXY(1, i)) != 0) {
01181         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01182         return false;
01183       }
01184     }
01185     for (uint i = 1; i < MapMaxY(); i++) {
01186       if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01187         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01188         return false;
01189       }
01190     }
01191     /* Make tiles at the border water again. */
01192     for (uint i = 0; i < MapMaxX(); i++) {
01193       SetTileHeight(TileXY(i, 0), 0);
01194       SetTileType(TileXY(i, 0), MP_WATER);
01195     }
01196     for (uint i = 0; i < MapMaxY(); i++) {
01197       SetTileHeight(TileXY(0, i), 0);
01198       SetTileType(TileXY(0, i), MP_WATER);
01199     }
01200   }
01201   MarkWholeScreenDirty();
01202   return true;
01203 }
01204 
01209 static bool ChangeDynamicEngines(int32 p1)
01210 {
01211   if (_game_mode == GM_MENU) return true;
01212 
01213   if (!EngineOverrideManager::ResetToCurrentNewGRFConfig()) {
01214     ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01215     return false;
01216   }
01217 
01218   return true;
01219 }
01220 
01221 static bool StationCatchmentChanged(int32 p1)
01222 {
01223   Station::RecomputeIndustriesNearForAll();
01224   return true;
01225 }
01226 
01227 
01228 #ifdef ENABLE_NETWORK
01229 
01230 static bool UpdateClientName(int32 p1)
01231 {
01232   NetworkUpdateClientName();
01233   return true;
01234 }
01235 
01236 static bool UpdateServerPassword(int32 p1)
01237 {
01238   if (strcmp(_settings_client.network.server_password, "*") == 0) {
01239     _settings_client.network.server_password[0] = '\0';
01240   }
01241 
01242   return true;
01243 }
01244 
01245 static bool UpdateRconPassword(int32 p1)
01246 {
01247   if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01248     _settings_client.network.rcon_password[0] = '\0';
01249   }
01250 
01251   return true;
01252 }
01253 
01254 static bool UpdateClientConfigValues(int32 p1)
01255 {
01256   if (_network_server) NetworkServerSendConfigUpdate();
01257 
01258   return true;
01259 }
01260 
01261 #endif /* ENABLE_NETWORK */
01262 
01263 
01264 /* End - Callback Functions */
01265 
01269 static void PrepareOldDiffCustom()
01270 {
01271   memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01272 }
01273 
01280 static void HandleOldDiffCustom(bool savegame)
01281 {
01282   uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01283 
01284   if (!savegame) {
01285     /* If we did read to old_diff_custom, then at least one value must be non 0. */
01286     bool old_diff_custom_used = false;
01287     for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01288       old_diff_custom_used = (_old_diff_custom[i] != 0);
01289     }
01290 
01291     if (!old_diff_custom_used) return;
01292   }
01293 
01294   for (uint i = 0; i < options_to_load; i++) {
01295     const SettingDesc *sd = &_settings[i];
01296     /* Skip deprecated options */
01297     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01298     void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01299     Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01300   }
01301 }
01302 
01309 static bool ConvertOldNewsSetting(const char *name, const char *value)
01310 {
01311   if (strcasecmp(name, "openclose") == 0) {
01312     /* openclose has been split in "open" and "close".
01313      * So the job is now to decrypt the value of the old news config
01314      * and give it to the two newly introduced ones*/
01315 
01316     NewsDisplay display = ND_OFF; // default
01317     if (strcasecmp(value, "full") == 0) {
01318       display = ND_FULL;
01319     } else if (strcasecmp(value, "summarized") == 0) {
01320       display = ND_SUMMARY;
01321     }
01322     /* tranfert of values */
01323     _news_type_data[NT_INDUSTRY_OPEN].display = display;
01324     _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01325     return true;
01326   }
01327   return false;
01328 }
01329 
01335 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01336 {
01337   IniGroup *group = ini->GetGroup(grpname);
01338   IniItem *item;
01339 
01340   /* If no group exists, return */
01341   if (group == NULL) return;
01342 
01343   for (item = group->item; item != NULL; item = item->next) {
01344     int news_item = -1;
01345     for (int i = 0; i < NT_END; i++) {
01346       if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01347         news_item = i;
01348         break;
01349       }
01350     }
01351 
01352     /* the config been read is not within current aceptable config */
01353     if (news_item == -1) {
01354       /* if the conversion function cannot process it, advice by a debug warning*/
01355       if (!ConvertOldNewsSetting(item->name, item->value)) {
01356         DEBUG(misc, 0, "Invalid display option: %s", item->name);
01357       }
01358       /* in all cases, there is nothing left to do */
01359       continue;
01360     }
01361 
01362     if (StrEmpty(item->value)) {
01363       DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01364       continue;
01365     } else if (strcasecmp(item->value, "full") == 0) {
01366       _news_type_data[news_item].display = ND_FULL;
01367     } else if (strcasecmp(item->value, "off") == 0) {
01368       _news_type_data[news_item].display = ND_OFF;
01369     } else if (strcasecmp(item->value, "summarized") == 0) {
01370       _news_type_data[news_item].display = ND_SUMMARY;
01371     } else {
01372       DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01373       continue;
01374     }
01375   }
01376 }
01377 
01378 static void AILoadConfig(IniFile *ini, const char *grpname)
01379 {
01380   IniGroup *group = ini->GetGroup(grpname);
01381   IniItem *item;
01382 
01383   /* Clean any configured AI */
01384   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01385     AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01386   }
01387 
01388   /* If no group exists, return */
01389   if (group == NULL) return;
01390 
01391   CompanyID c = COMPANY_FIRST;
01392   for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01393     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01394 
01395     config->Change(item->name);
01396     if (!config->HasScript()) {
01397       if (strcmp(item->name, "none") != 0) {
01398         DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01399         continue;
01400       }
01401     }
01402     if (item->value != NULL) config->StringToSettings(item->value);
01403   }
01404 }
01405 
01406 static void GameLoadConfig(IniFile *ini, const char *grpname)
01407 {
01408   IniGroup *group = ini->GetGroup(grpname);
01409   IniItem *item;
01410 
01411   /* Clean any configured GameScript */
01412   GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(NULL);
01413 
01414   /* If no group exists, return */
01415   if (group == NULL) return;
01416 
01417   item = group->item;
01418   if (item == NULL) return;
01419 
01420   GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01421 
01422   config->Change(item->name);
01423   if (!config->HasScript()) {
01424     if (strcmp(item->name, "none") != 0) {
01425       DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
01426       return;
01427     }
01428   }
01429   if (item->value != NULL) config->StringToSettings(item->value);
01430 }
01431 
01438 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01439 {
01440   IniGroup *group = ini->GetGroup(grpname);
01441   IniItem *item;
01442   GRFConfig *first = NULL;
01443   GRFConfig **curr = &first;
01444 
01445   if (group == NULL) return NULL;
01446 
01447   for (item = group->item; item != NULL; item = item->next) {
01448     GRFConfig *c = new GRFConfig(item->name);
01449 
01450     /* Parse parameters */
01451     if (!StrEmpty(item->value)) {
01452       c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01453       if (c->num_params == (byte)-1) {
01454         SetDParamStr(0, item->name);
01455         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
01456         c->num_params = 0;
01457       }
01458     }
01459 
01460     /* Check if item is valid */
01461     if (!FillGRFDetails(c, is_static) || HasBit(c->flags, GCF_INVALID)) {
01462       if (c->status == GCS_NOT_FOUND) {
01463         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_NOT_FOUND);
01464       } else if (HasBit(c->flags, GCF_UNSAFE)) {
01465         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNSAFE);
01466       } else if (HasBit(c->flags, GCF_SYSTEM)) {
01467         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_SYSTEM);
01468       } else if (HasBit(c->flags, GCF_INVALID)) {
01469         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_INCOMPATIBLE);
01470       } else {
01471         SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
01472       }
01473 
01474       SetDParamStr(0, item->name);
01475       ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
01476       delete c;
01477       continue;
01478     }
01479 
01480     /* Check for duplicate GRFID (will also check for duplicate filenames) */
01481     bool duplicate = false;
01482     for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01483       if (gc->ident.grfid == c->ident.grfid) {
01484         SetDParamStr(0, item->name);
01485         SetDParamStr(1, gc->filename);
01486         ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_DUPLICATE_GRFID, WL_CRITICAL);
01487         duplicate = true;
01488         break;
01489       }
01490     }
01491     if (duplicate) {
01492       delete c;
01493       continue;
01494     }
01495 
01496     /* Mark file as static to avoid saving in savegame. */
01497     if (is_static) SetBit(c->flags, GCF_STATIC);
01498 
01499     /* Add item to list */
01500     *curr = c;
01501     curr = &c->next;
01502   }
01503 
01504   return first;
01505 }
01506 
01512 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01513 {
01514   IniGroup *group = ini->GetGroup(grpname);
01515 
01516   for (int i = 0; i < NT_END; i++) {
01517     const char *value;
01518     int v = _news_type_data[i].display;
01519 
01520     value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01521 
01522     group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01523   }
01524 }
01525 
01526 static void AISaveConfig(IniFile *ini, const char *grpname)
01527 {
01528   IniGroup *group = ini->GetGroup(grpname);
01529 
01530   if (group == NULL) return;
01531   group->Clear();
01532 
01533   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01534     AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
01535     const char *name;
01536     char value[1024];
01537     config->SettingsToString(value, lengthof(value));
01538 
01539     if (config->HasScript()) {
01540       name = config->GetName();
01541     } else {
01542       name = "none";
01543     }
01544 
01545     IniItem *item = new IniItem(group, name, strlen(name));
01546     item->SetValue(value);
01547   }
01548 }
01549 
01550 static void GameSaveConfig(IniFile *ini, const char *grpname)
01551 {
01552   IniGroup *group = ini->GetGroup(grpname);
01553 
01554   if (group == NULL) return;
01555   group->Clear();
01556 
01557   GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
01558   const char *name;
01559   char value[1024];
01560   config->SettingsToString(value, lengthof(value));
01561 
01562   if (config->HasScript()) {
01563     name = config->GetName();
01564   } else {
01565     name = "none";
01566   }
01567 
01568   IniItem *item = new IniItem(group, name, strlen(name));
01569   item->SetValue(value);
01570 }
01571 
01576 static void SaveVersionInConfig(IniFile *ini)
01577 {
01578   IniGroup *group = ini->GetGroup("version");
01579 
01580   char version[9];
01581   snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01582 
01583   const char * const versions[][2] = {
01584     { "version_string", _openttd_revision },
01585     { "version_number", version }
01586   };
01587 
01588   for (uint i = 0; i < lengthof(versions); i++) {
01589     group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01590   }
01591 }
01592 
01593 /* Save a GRF configuration to the given group name */
01594 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01595 {
01596   ini->RemoveGroup(grpname);
01597   IniGroup *group = ini->GetGroup(grpname);
01598   const GRFConfig *c;
01599 
01600   for (c = list; c != NULL; c = c->next) {
01601     char params[512];
01602     GRFBuildParamList(params, c, lastof(params));
01603 
01604     group->GetItem(c->filename, true)->SetValue(params);
01605   }
01606 }
01607 
01608 /* Common handler for saving/loading variables to the configuration file */
01609 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool basic_settings = true, bool other_settings = true)
01610 {
01611   if (basic_settings) {
01612     proc(ini, (const SettingDesc*)_misc_settings,    "misc",  NULL);
01613 #if defined(WIN32) && !defined(DEDICATED)
01614     proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
01615 #endif /* WIN32 */
01616   }
01617 
01618   if (other_settings) {
01619     proc(ini, _settings,         "patches",  &_settings_newgame);
01620     proc(ini, _currency_settings,"currency", &_custom_currency);
01621     proc(ini, _company_settings, "company",  &_settings_client.company);
01622 
01623 #ifdef ENABLE_NETWORK
01624     proc_list(ini, "server_bind_addresses", &_network_bind_list);
01625     proc_list(ini, "servers", &_network_host_list);
01626     proc_list(ini, "bans",    &_network_ban_list);
01627 #endif /* ENABLE_NETWORK */
01628   }
01629 }
01630 
01631 static IniFile *IniLoadConfig()
01632 {
01633   IniFile *ini = new IniFile(_list_group_names);
01634   ini->LoadFromDisk(_config_file, BASE_DIR);
01635   return ini;
01636 }
01637 
01642 void LoadFromConfig(bool minimal)
01643 {
01644   IniFile *ini = IniLoadConfig();
01645   if (!minimal) ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
01646 
01647   /* Load basic settings only during bootstrap, load other settings not during bootstrap */
01648   HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList, minimal, !minimal);
01649 
01650   if (!minimal) {
01651     _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01652     _grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
01653     NewsDisplayLoadConfig(ini, "news_display");
01654     AILoadConfig(ini, "ai_players");
01655     GameLoadConfig(ini, "game_scripts");
01656 
01657     PrepareOldDiffCustom();
01658     IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01659     HandleOldDiffCustom(false);
01660 
01661     ValidateSettings();
01662 
01663     /* Display sheduled errors */
01664     extern void ScheduleErrorMessage(ErrorList &datas);
01665     ScheduleErrorMessage(_settings_error_list);
01666     if (FindWindowById(WC_ERRMSG, 0) == NULL) ShowFirstError();
01667   }
01668 
01669   delete ini;
01670 }
01671 
01673 void SaveToConfig()
01674 {
01675   IniFile *ini = IniLoadConfig();
01676 
01677   /* Remove some obsolete groups. These have all been loaded into other groups. */
01678   ini->RemoveGroup("patches");
01679   ini->RemoveGroup("yapf");
01680   ini->RemoveGroup("gameopt");
01681 
01682   HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01683   GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01684   GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01685   NewsDisplaySaveConfig(ini, "news_display");
01686   AISaveConfig(ini, "ai_players");
01687   GameSaveConfig(ini, "game_scripts");
01688   SaveVersionInConfig(ini);
01689   ini->SaveToDisk(_config_file);
01690   delete ini;
01691 }
01692 
01697 void GetGRFPresetList(GRFPresetList *list)
01698 {
01699   list->Clear();
01700 
01701   IniFile *ini = IniLoadConfig();
01702   IniGroup *group;
01703   for (group = ini->group; group != NULL; group = group->next) {
01704     if (strncmp(group->name, "preset-", 7) == 0) {
01705       *list->Append() = strdup(group->name + 7);
01706     }
01707   }
01708 
01709   delete ini;
01710 }
01711 
01718 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01719 {
01720   char *section = (char*)alloca(strlen(config_name) + 8);
01721   sprintf(section, "preset-%s", config_name);
01722 
01723   IniFile *ini = IniLoadConfig();
01724   GRFConfig *config = GRFLoadConfig(ini, section, false);
01725   delete ini;
01726 
01727   return config;
01728 }
01729 
01736 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01737 {
01738   char *section = (char*)alloca(strlen(config_name) + 8);
01739   sprintf(section, "preset-%s", config_name);
01740 
01741   IniFile *ini = IniLoadConfig();
01742   GRFSaveConfig(ini, section, config);
01743   ini->SaveToDisk(_config_file);
01744   delete ini;
01745 }
01746 
01751 void DeleteGRFPresetFromConfig(const char *config_name)
01752 {
01753   char *section = (char*)alloca(strlen(config_name) + 8);
01754   sprintf(section, "preset-%s", config_name);
01755 
01756   IniFile *ini = IniLoadConfig();
01757   ini->RemoveGroup(section);
01758   ini->SaveToDisk(_config_file);
01759   delete ini;
01760 }
01761 
01762 const SettingDesc *GetSettingDescription(uint index)
01763 {
01764   if (index >= lengthof(_settings)) return NULL;
01765   return &_settings[index];
01766 }
01767 
01779 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01780 {
01781   const SettingDesc *sd = GetSettingDescription(p1);
01782 
01783   if (sd == NULL) return CMD_ERROR;
01784   if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01785 
01786   if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01787   if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01788   if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01789       (_game_mode == GM_NORMAL ||
01790       (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01791     return CMD_ERROR;
01792   }
01793 
01794   if (flags & DC_EXEC) {
01795     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01796 
01797     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01798     int32 newval = (int32)p2;
01799 
01800     Write_ValidateSetting(var, sd, newval);
01801     newval = (int32)ReadValue(var, sd->save.conv);
01802 
01803     if (oldval == newval) return CommandCost();
01804 
01805     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01806       WriteValue(var, sd->save.conv, (int64)oldval);
01807       return CommandCost();
01808     }
01809 
01810     if (sd->desc.flags & SGF_NO_NETWORK) {
01811       GamelogStartAction(GLAT_SETTING);
01812       GamelogSetting(sd->desc.name, oldval, newval);
01813       GamelogStopAction();
01814     }
01815 
01816     SetWindowClassesDirty(WC_GAME_OPTIONS);
01817   }
01818 
01819   return CommandCost();
01820 }
01821 
01832 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01833 {
01834   if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01835   const SettingDesc *sd = &_company_settings[p1];
01836 
01837   if (flags & DC_EXEC) {
01838     void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01839 
01840     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01841     int32 newval = (int32)p2;
01842 
01843     Write_ValidateSetting(var, sd, newval);
01844     newval = (int32)ReadValue(var, sd->save.conv);
01845 
01846     if (oldval == newval) return CommandCost();
01847 
01848     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01849       WriteValue(var, sd->save.conv, (int64)oldval);
01850       return CommandCost();
01851     }
01852 
01853     SetWindowClassesDirty(WC_GAME_OPTIONS);
01854   }
01855 
01856   return CommandCost();
01857 }
01858 
01866 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01867 {
01868   const SettingDesc *sd = &_settings[index];
01869   /* If an item is company-based, we do not send it over the network
01870    * (if any) to change. Also *hack*hack* we update the _newgame version
01871    * of settings because changing a company-based setting in a game also
01872    * changes its defaults. At least that is the convention we have chosen */
01873   if (sd->save.conv & SLF_NO_NETWORK_SYNC) {
01874     void *var = GetVariableAddress(&GetGameSettings(), &sd->save);
01875     Write_ValidateSetting(var, sd, value);
01876 
01877     if (_game_mode != GM_MENU) {
01878       void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01879       Write_ValidateSetting(var2, sd, value);
01880     }
01881     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01882 
01883     SetWindowClassesDirty(WC_GAME_OPTIONS);
01884 
01885     return true;
01886   }
01887 
01888   if (force_newgame) {
01889     void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01890     Write_ValidateSetting(var2, sd, value);
01891     return true;
01892   }
01893 
01894   /* send non-company-based settings over the network */
01895   if (!_networking || (_networking && _network_server)) {
01896     return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01897   }
01898   return false;
01899 }
01900 
01907 void SetCompanySetting(uint index, int32 value)
01908 {
01909   const SettingDesc *sd = &_company_settings[index];
01910   if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01911     DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01912   } else {
01913     void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01914     Write_ValidateSetting(var, sd, value);
01915     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01916   }
01917 }
01918 
01922 void SetDefaultCompanySettings(CompanyID cid)
01923 {
01924   Company *c = Company::Get(cid);
01925   const SettingDesc *sd;
01926   for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01927     void *var = GetVariableAddress(&c->settings, &sd->save);
01928     Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01929   }
01930 }
01931 
01932 #if defined(ENABLE_NETWORK)
01933 
01936 void SyncCompanySettings()
01937 {
01938   const SettingDesc *sd;
01939   uint i = 0;
01940   for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01941     const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01942     const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01943     uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01944     uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01945     if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01946   }
01947 }
01948 #endif /* ENABLE_NETWORK */
01949 
01955 uint GetCompanySettingIndex(const char *name)
01956 {
01957   uint i;
01958   const SettingDesc *sd = GetSettingFromName(name, &i);
01959   assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01960   return i;
01961 }
01962 
01970 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01971 {
01972   const SettingDesc *sd = &_settings[index];
01973   assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
01974 
01975   if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01976     char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01977     free(*var);
01978     *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01979   } else {
01980     char *var = (char*)GetVariableAddress(NULL, &sd->save);
01981     ttd_strlcpy(var, value, sd->save.length);
01982   }
01983   if (sd->desc.proc != NULL) sd->desc.proc(0);
01984 
01985   return true;
01986 }
01987 
01995 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01996 {
01997   const SettingDesc *sd;
01998 
01999   /* First check all full names */
02000   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02001     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02002     if (strcmp(sd->desc.name, name) == 0) return sd;
02003   }
02004 
02005   /* Then check the shortcut variant of the name. */
02006   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02007     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02008     const char *short_name = strchr(sd->desc.name, '.');
02009     if (short_name != NULL) {
02010       short_name++;
02011       if (strcmp(short_name, name) == 0) return sd;
02012     }
02013   }
02014 
02015   if (strncmp(name, "company.", 8) == 0) name += 8;
02016   /* And finally the company-based settings */
02017   for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
02018     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02019     if (strcmp(sd->desc.name, name) == 0) return sd;
02020   }
02021 
02022   return NULL;
02023 }
02024 
02025 /* Those 2 functions need to be here, else we have to make some stuff non-static
02026  * and besides, it is also better to keep stuff like this at the same place */
02027 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
02028 {
02029   uint index;
02030   const SettingDesc *sd = GetSettingFromName(name, &index);
02031 
02032   if (sd == NULL) {
02033     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02034     return;
02035   }
02036 
02037   bool success;
02038   if (sd->desc.cmd == SDT_STRING) {
02039     success = SetSettingValue(index, value, force_newgame);
02040   } else {
02041     uint32 val;
02042     extern bool GetArgumentInteger(uint32 *value, const char *arg);
02043     success = GetArgumentInteger(&val, value);
02044     if (!success) {
02045       IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
02046       return;
02047     }
02048 
02049     success = SetSettingValue(index, val, force_newgame);
02050   }
02051 
02052   if (!success) {
02053     if (_network_server) {
02054       IConsoleError("This command/variable is not available during network games.");
02055     } else {
02056       IConsoleError("This command/variable is only available to a network server.");
02057     }
02058   }
02059 }
02060 
02061 void IConsoleSetSetting(const char *name, int value)
02062 {
02063   uint index;
02064   const SettingDesc *sd = GetSettingFromName(name, &index);
02065   assert(sd != NULL);
02066   SetSettingValue(index, value);
02067 }
02068 
02074 void IConsoleGetSetting(const char *name, bool force_newgame)
02075 {
02076   char value[20];
02077   uint index;
02078   const SettingDesc *sd = GetSettingFromName(name, &index);
02079   const void *ptr;
02080 
02081   if (sd == NULL) {
02082     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
02083     return;
02084   }
02085 
02086   ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
02087 
02088   if (sd->desc.cmd == SDT_STRING) {
02089     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02090   } else {
02091     if (sd->desc.cmd == SDT_BOOLX) {
02092       snprintf(value, sizeof(value), (*(const bool*)ptr != 0) ? "on" : "off");
02093     } else {
02094       snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02095     }
02096 
02097     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
02098       name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
02099   }
02100 }
02101 
02107 void IConsoleListSettings(const char *prefilter)
02108 {
02109   IConsolePrintF(CC_WARNING, "All settings with their current value:");
02110 
02111   for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
02112     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
02113     if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
02114     char value[80];
02115     const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
02116 
02117     if (sd->desc.cmd == SDT_BOOLX) {
02118       snprintf(value, lengthof(value), (*(const bool *)ptr != 0) ? "on" : "off");
02119     } else if (sd->desc.cmd == SDT_STRING) {
02120       snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
02121     } else {
02122       snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
02123     }
02124     IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
02125   }
02126 
02127   IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
02128 }
02129 
02136 static void LoadSettings(const SettingDesc *osd, void *object)
02137 {
02138   for (; osd->save.cmd != SL_END; osd++) {
02139     const SaveLoad *sld = &osd->save;
02140     void *ptr = GetVariableAddress(object, sld);
02141 
02142     if (!SlObjectMember(ptr, sld)) continue;
02143     if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
02144   }
02145 }
02146 
02153 static void SaveSettings(const SettingDesc *sd, void *object)
02154 {
02155   /* We need to write the CH_RIFF header, but unfortunately can't call
02156    * SlCalcLength() because we have a different format. So do this manually */
02157   const SettingDesc *i;
02158   size_t length = 0;
02159   for (i = sd; i->save.cmd != SL_END; i++) {
02160     length += SlCalcObjMemberLength(object, &i->save);
02161   }
02162   SlSetLength(length);
02163 
02164   for (i = sd; i->save.cmd != SL_END; i++) {
02165     void *ptr = GetVariableAddress(object, &i->save);
02166     SlObjectMember(ptr, &i->save);
02167   }
02168 }
02169 
02170 static void Load_OPTS()
02171 {
02172   /* Copy over default setting since some might not get loaded in
02173    * a networking environment. This ensures for example that the local
02174    * autosave-frequency stays when joining a network-server */
02175   PrepareOldDiffCustom();
02176   LoadSettings(_gameopt_settings, &_settings_game);
02177   HandleOldDiffCustom(true);
02178 }
02179 
02180 static void Load_PATS()
02181 {
02182   /* Copy over default setting since some might not get loaded in
02183    * a networking environment. This ensures for example that the local
02184    * currency setting stays when joining a network-server */
02185   LoadSettings(_settings, &_settings_game);
02186 }
02187 
02188 static void Check_PATS()
02189 {
02190   LoadSettings(_settings, &_load_check_data.settings);
02191 }
02192 
02193 static void Save_PATS()
02194 {
02195   SaveSettings(_settings, &_settings_game);
02196 }
02197 
02198 void CheckConfig()
02199 {
02200   /*
02201    * Increase old default values for pf_maxdepth and pf_maxlength
02202    * to support big networks.
02203    */
02204   if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02205     _settings_newgame.pf.opf.pf_maxdepth = 48;
02206     _settings_newgame.pf.opf.pf_maxlength = 4096;
02207   }
02208 }
02209 
02210 extern const ChunkHandler _setting_chunk_handlers[] = {
02211   { 'OPTS', NULL,      Load_OPTS, NULL, NULL,       CH_RIFF},
02212   { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02213 };
02214 
02215 static bool IsSignedVarMemType(VarType vt)
02216 {
02217   switch (GetVarMemType(vt)) {
02218     case SLE_VAR_I8:
02219     case SLE_VAR_I16:
02220     case SLE_VAR_I32:
02221     case SLE_VAR_I64:
02222       return true;
02223   }
02224   return false;
02225 }