settings.cpp

Go to the documentation of this file.
00001 /* $Id: settings.cpp 21523 2010-12-15 09:01:04Z terkhen $ */
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 "strings_func.h"
00040 #include "vehicle_func.h"
00041 #include "sound_func.h"
00042 #include "company_func.h"
00043 #include "rev.h"
00044 #ifdef WITH_FREETYPE
00045 #include "fontcache.h"
00046 #endif
00047 #include "textbuf_gui.h"
00048 #include "rail_gui.h"
00049 #include "elrail_func.h"
00050 #include "gui.h"
00051 #include "town.h"
00052 #include "video/video_driver.hpp"
00053 #include "sound/sound_driver.hpp"
00054 #include "music/music_driver.hpp"
00055 #include "blitter/factory.hpp"
00056 #include "base_media_base.h"
00057 #include "gamelog.h"
00058 #include "settings_func.h"
00059 #include "ini_type.h"
00060 #include "ai/ai_config.hpp"
00061 #include "ai/ai.hpp"
00062 #include "newgrf.h"
00063 #include "ship.h"
00064 #include "smallmap_gui.h"
00065 #include "roadveh.h"
00066 #include "fios.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 void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00081 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00082 
00083 static bool IsSignedVarMemType(VarType vt);
00084 
00088 static const char * const _list_group_names[] = {
00089   "bans",
00090   "newgrf",
00091   "servers",
00092   "server_bind_addresses",
00093   NULL
00094 };
00095 
00103 static int LookupOneOfMany(const char *many, const char *one, size_t onelen = 0)
00104 {
00105   const char *s;
00106   int idx;
00107 
00108   if (onelen == 0) onelen = strlen(one);
00109 
00110   /* check if it's an integer */
00111   if (*one >= '0' && *one <= '9') return strtoul(one, NULL, 0);
00112 
00113   idx = 0;
00114   for (;;) {
00115     /* find end of item */
00116     s = many;
00117     while (*s != '|' && *s != 0) s++;
00118     if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00119     if (*s == 0) return -1;
00120     many = s + 1;
00121     idx++;
00122   }
00123 }
00124 
00132 static uint32 LookupManyOfMany(const char *many, const char *str)
00133 {
00134   const char *s;
00135   int r;
00136   uint32 res = 0;
00137 
00138   for (;;) {
00139     /* skip "whitespace" */
00140     while (*str == ' ' || *str == '\t' || *str == '|') str++;
00141     if (*str == 0) break;
00142 
00143     s = str;
00144     while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00145 
00146     r = LookupOneOfMany(many, str, s - str);
00147     if (r == -1) return (uint32)-1;
00148 
00149     SetBit(res, r); // value found, set it
00150     if (*s == 0) break;
00151     str = s + 1;
00152   }
00153   return res;
00154 }
00155 
00164 static int ParseIntList(const char *p, int *items, int maxitems)
00165 {
00166   int n = 0; // number of items read so far
00167   bool comma = false; // do we accept comma?
00168 
00169   while (*p != '\0') {
00170     switch (*p) {
00171       case ',':
00172         /* Do not accept multiple commas between numbers */
00173         if (!comma) return -1;
00174         comma = false;
00175         /* FALL THROUGH */
00176       case ' ':
00177         p++;
00178         break;
00179 
00180       default: {
00181         if (n == maxitems) return -1; // we don't accept that many numbers
00182         char *end;
00183         long v = strtol(p, &end, 0);
00184         if (p == end) return -1; // invalid character (not a number)
00185         if (sizeof(int) < sizeof(long)) v = ClampToI32(v);
00186         items[n++] = v;
00187         p = end; // first non-number
00188         comma = true; // we accept comma now
00189         break;
00190       }
00191     }
00192   }
00193 
00194   /* If we have read comma but no number after it, fail.
00195    * We have read comma when (n != 0) and comma is not allowed */
00196   if (n != 0 && !comma) return -1;
00197 
00198   return n;
00199 }
00200 
00209 static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
00210 {
00211   int items[64];
00212   int i, nitems;
00213 
00214   if (str == NULL) {
00215     memset(items, 0, sizeof(items));
00216     nitems = nelems;
00217   } else {
00218     nitems = ParseIntList(str, items, lengthof(items));
00219     if (nitems != nelems) return false;
00220   }
00221 
00222   switch (type) {
00223   case SLE_VAR_BL:
00224   case SLE_VAR_I8:
00225   case SLE_VAR_U8:
00226     for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00227     break;
00228   case SLE_VAR_I16:
00229   case SLE_VAR_U16:
00230     for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00231     break;
00232   case SLE_VAR_I32:
00233   case SLE_VAR_U32:
00234     for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00235     break;
00236   default: NOT_REACHED();
00237   }
00238 
00239   return true;
00240 }
00241 
00251 static void MakeIntList(char *buf, const char *last, const void *array, int nelems, VarType type)
00252 {
00253   int i, v = 0;
00254   byte *p = (byte*)array;
00255 
00256   for (i = 0; i != nelems; i++) {
00257     switch (type) {
00258     case SLE_VAR_BL:
00259     case SLE_VAR_I8:  v = *(int8*)p;   p += 1; break;
00260     case SLE_VAR_U8:  v = *(byte*)p;   p += 1; break;
00261     case SLE_VAR_I16: v = *(int16*)p;  p += 2; break;
00262     case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00263     case SLE_VAR_I32: v = *(int32*)p;  p += 4; break;
00264     case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00265     default: NOT_REACHED();
00266     }
00267     buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00268   }
00269 }
00270 
00278 static void MakeOneOfMany(char *buf, const char *last, const char *many, int id)
00279 {
00280   int orig_id = id;
00281 
00282   /* Look for the id'th element */
00283   while (--id >= 0) {
00284     for (; *many != '|'; many++) {
00285       if (*many == '\0') { // not found
00286         seprintf(buf, last, "%d", orig_id);
00287         return;
00288       }
00289     }
00290     many++; // pass the |-character
00291   }
00292 
00293   /* copy string until next item (|) or the end of the list if this is the last one */
00294   while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00295   *buf = '\0';
00296 }
00297 
00306 static void MakeManyOfMany(char *buf, const char *last, const char *many, uint32 x)
00307 {
00308   const char *start;
00309   int i = 0;
00310   bool init = true;
00311 
00312   for (; x != 0; x >>= 1, i++) {
00313     start = many;
00314     while (*many != 0 && *many != '|') many++; // advance to the next element
00315 
00316     if (HasBit(x, 0)) { // item found, copy it
00317       if (!init) buf += seprintf(buf, last, "|");
00318       init = false;
00319       if (start == many) {
00320         buf += seprintf(buf, last, "%d", i);
00321       } else {
00322         memcpy(buf, start, many - start);
00323         buf += many - start;
00324       }
00325     }
00326 
00327     if (*many == '|') many++;
00328   }
00329 
00330   *buf = '\0';
00331 }
00332 
00339 static const void *StringToVal(const SettingDescBase *desc, const char *orig_str)
00340 {
00341   const char *str = orig_str == NULL ? "" : orig_str;
00342   switch (desc->cmd) {
00343   case SDT_NUMX: {
00344     char *end;
00345     unsigned long val = strtoul(str, &end, 0);
00346     if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00347     return (void*)val;
00348   }
00349   case SDT_ONEOFMANY: {
00350     long r = LookupOneOfMany(desc->many, str);
00351     /* if the first attempt of conversion from string to the appropriate value fails,
00352      * look if we have defined a converter from old value to new value. */
00353     if (r == -1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00354     if (r != -1) return (void*)r; // and here goes converted value
00355     ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name); // sorry, we failed
00356     return 0;
00357   }
00358   case SDT_MANYOFMANY: {
00359     unsigned long r = LookupManyOfMany(desc->many, str);
00360     if (r != (unsigned long)-1) return (void*)r;
00361     ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00362     return 0;
00363   }
00364   case SDT_BOOLX:
00365     if (strcmp(str, "true")  == 0 || strcmp(str, "on")  == 0 || strcmp(str, "1") == 0) return (void*)true;
00366     if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return (void*)false;
00367     ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00368     break;
00369 
00370   case SDT_STRING: return orig_str;
00371   case SDT_INTLIST: return str;
00372   default: break;
00373   }
00374 
00375   return NULL;
00376 }
00377 
00387 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00388 {
00389   const SettingDescBase *sdb = &sd->desc;
00390 
00391   if (sdb->cmd != SDT_BOOLX &&
00392       sdb->cmd != SDT_NUMX &&
00393       sdb->cmd != SDT_ONEOFMANY &&
00394       sdb->cmd != SDT_MANYOFMANY) {
00395     return;
00396   }
00397 
00398   /* We cannot know the maximum value of a bitset variable, so just have faith */
00399   if (sdb->cmd != SDT_MANYOFMANY) {
00400     /* We need to take special care of the uint32 type as we receive from the function
00401      * a signed integer. While here also bail out on 64-bit settings as those are not
00402      * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
00403      * 32-bit variable
00404      * TODO: Support 64-bit settings/variables */
00405     switch (GetVarMemType(sd->save.conv)) {
00406       case SLE_VAR_NULL: return;
00407       case SLE_VAR_BL:
00408       case SLE_VAR_I8:
00409       case SLE_VAR_U8:
00410       case SLE_VAR_I16:
00411       case SLE_VAR_U16:
00412       case SLE_VAR_I32: {
00413         /* Override the minimum value. No value below sdb->min, except special value 0 */
00414         if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00415         break;
00416       }
00417       case SLE_VAR_U32: {
00418         /* Override the minimum value. No value below sdb->min, except special value 0 */
00419         uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00420         WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00421         return;
00422       }
00423       case SLE_VAR_I64:
00424       case SLE_VAR_U64:
00425       default: NOT_REACHED();
00426     }
00427   }
00428 
00429   WriteValue(ptr, sd->save.conv, (int64)val);
00430 }
00431 
00440 static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00441 {
00442   IniGroup *group;
00443   IniGroup *group_def = ini->GetGroup(grpname);
00444   IniItem *item;
00445   const void *p;
00446   void *ptr;
00447   const char *s;
00448 
00449   for (; sd->save.cmd != SL_END; sd++) {
00450     const SettingDescBase *sdb = &sd->desc;
00451     const SaveLoad        *sld = &sd->save;
00452 
00453     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00454 
00455     /* For settings.xx.yy load the settings from [xx] yy = ? */
00456     s = strchr(sdb->name, '.');
00457     if (s != NULL) {
00458       group = ini->GetGroup(sdb->name, s - sdb->name);
00459       s++;
00460     } else {
00461       s = sdb->name;
00462       group = group_def;
00463     }
00464 
00465     item = group->GetItem(s, false);
00466     if (item == NULL && group != group_def) {
00467       /* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
00468        * did not exist (e.g. loading old config files with a [settings] section */
00469       item = group_def->GetItem(s, false);
00470     }
00471     if (item == NULL) {
00472       /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
00473        * did not exist (e.g. loading old config files with a [yapf] section */
00474       const char *sc = strchr(s, '.');
00475       if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00476     }
00477 
00478     p = (item == NULL) ? sdb->def : StringToVal(sdb, item->value);
00479     ptr = GetVariableAddress(object, sld);
00480 
00481     switch (sdb->cmd) {
00482     case SDT_BOOLX: // All four are various types of (integer) numbers
00483     case SDT_NUMX:
00484     case SDT_ONEOFMANY:
00485     case SDT_MANYOFMANY:
00486       Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00487 
00488     case SDT_STRING:
00489       switch (GetVarMemType(sld->conv)) {
00490         case SLE_VAR_STRB:
00491         case SLE_VAR_STRBQ:
00492           if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00493           break;
00494         case SLE_VAR_STR:
00495         case SLE_VAR_STRQ:
00496           free(*(char**)ptr);
00497           *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00498           break;
00499         case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00500         default: NOT_REACHED();
00501       }
00502       break;
00503 
00504     case SDT_INTLIST: {
00505       if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00506         ShowInfoF("ini: error in array '%s'", sdb->name);
00507       } else if (sd->desc.proc_cnvt != NULL) {
00508         sd->desc.proc_cnvt((const char*)p);
00509       }
00510       break;
00511     }
00512     default: NOT_REACHED();
00513     }
00514   }
00515 }
00516 
00529 static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00530 {
00531   IniGroup *group_def = NULL, *group;
00532   IniItem *item;
00533   char buf[512];
00534   const char *s;
00535   void *ptr;
00536 
00537   for (; sd->save.cmd != SL_END; sd++) {
00538     const SettingDescBase *sdb = &sd->desc;
00539     const SaveLoad        *sld = &sd->save;
00540 
00541     /* If the setting is not saved to the configuration
00542      * file, just continue with the next setting */
00543     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00544     if (sld->conv & SLF_CONFIG_NO) continue;
00545 
00546     /* XXX - wtf is this?? (group override?) */
00547     s = strchr(sdb->name, '.');
00548     if (s != NULL) {
00549       group = ini->GetGroup(sdb->name, s - sdb->name);
00550       s++;
00551     } else {
00552       if (group_def == NULL) group_def = ini->GetGroup(grpname);
00553       s = sdb->name;
00554       group = group_def;
00555     }
00556 
00557     item = group->GetItem(s, true);
00558     ptr = GetVariableAddress(object, sld);
00559 
00560     if (item->value != NULL) {
00561       /* check if the value is the same as the old value */
00562       const void *p = StringToVal(sdb, item->value);
00563 
00564       /* The main type of a variable/setting is in bytes 8-15
00565        * The subtype (what kind of numbers do we have there) is in 0-7 */
00566       switch (sdb->cmd) {
00567       case SDT_BOOLX:
00568       case SDT_NUMX:
00569       case SDT_ONEOFMANY:
00570       case SDT_MANYOFMANY:
00571         switch (GetVarMemType(sld->conv)) {
00572         case SLE_VAR_BL:
00573           if (*(bool*)ptr == (p != NULL)) continue;
00574           break;
00575         case SLE_VAR_I8:
00576         case SLE_VAR_U8:
00577           if (*(byte*)ptr == (byte)(unsigned long)p) continue;
00578           break;
00579         case SLE_VAR_I16:
00580         case SLE_VAR_U16:
00581           if (*(uint16*)ptr == (uint16)(unsigned long)p) continue;
00582           break;
00583         case SLE_VAR_I32:
00584         case SLE_VAR_U32:
00585           if (*(uint32*)ptr == (uint32)(unsigned long)p) continue;
00586           break;
00587         default: NOT_REACHED();
00588         }
00589         break;
00590       default: break; // Assume the other types are always changed
00591       }
00592     }
00593 
00594     /* Value has changed, get the new value and put it into a buffer */
00595     switch (sdb->cmd) {
00596     case SDT_BOOLX:
00597     case SDT_NUMX:
00598     case SDT_ONEOFMANY:
00599     case SDT_MANYOFMANY: {
00600       uint32 i = (uint32)ReadValue(ptr, sld->conv);
00601 
00602       switch (sdb->cmd) {
00603       case SDT_BOOLX:      strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00604       case SDT_NUMX:       seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00605       case SDT_ONEOFMANY:  MakeOneOfMany(buf, lastof(buf), sdb->many, i); break;
00606       case SDT_MANYOFMANY: MakeManyOfMany(buf, lastof(buf), sdb->many, i); break;
00607       default: NOT_REACHED();
00608       }
00609       break;
00610     }
00611 
00612     case SDT_STRING:
00613       switch (GetVarMemType(sld->conv)) {
00614       case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00615       case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00616       case SLE_VAR_STR:  strecpy(buf, *(char**)ptr, lastof(buf)); break;
00617       case SLE_VAR_STRQ:
00618         if (*(char**)ptr == NULL) {
00619           buf[0] = '\0';
00620         } else {
00621           seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00622         }
00623         break;
00624       case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00625       default: NOT_REACHED();
00626       }
00627       break;
00628 
00629     case SDT_INTLIST:
00630       MakeIntList(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00631       break;
00632     default: NOT_REACHED();
00633     }
00634 
00635     /* The value is different, that means we have to write it to the ini */
00636     free(item->value);
00637     item->value = strdup(buf);
00638   }
00639 }
00640 
00650 static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
00651 {
00652   IniGroup *group = ini->GetGroup(grpname);
00653 
00654   if (group == NULL || list == NULL) return;
00655 
00656   list->Clear();
00657 
00658   for (const IniItem *item = group->item; item != NULL; item = item->next) {
00659     if (item->name != NULL) *list->Append() = strdup(item->name);
00660   }
00661 }
00662 
00672 static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
00673 {
00674   IniGroup *group = ini->GetGroup(grpname);
00675 
00676   if (group == NULL || list == NULL) return;
00677   group->Clear();
00678 
00679   for (char **iter = list->Begin(); iter != list->End(); iter++) {
00680     group->GetItem(*iter, true)->SetValue("");
00681   }
00682 }
00683 
00684 /* Begin - Callback Functions for the various settings. */
00685 
00687 static bool v_PositionMainToolbar(int32 p1)
00688 {
00689   if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00690   return true;
00691 }
00692 
00694 static bool v_PositionStatusbar(int32 p1)
00695 {
00696   if (_game_mode != GM_MENU) {
00697     PositionStatusbar(NULL);
00698     PositionNewsMessage(NULL);
00699   }
00700   return true;
00701 }
00702 
00703 static bool PopulationInLabelActive(int32 p1)
00704 {
00705   UpdateAllTownVirtCoords();
00706   return true;
00707 }
00708 
00709 static bool RedrawScreen(int32 p1)
00710 {
00711   MarkWholeScreenDirty();
00712   return true;
00713 }
00714 
00720 static bool RedrawSmallmap(int32 p1)
00721 {
00722   BuildLandLegend();
00723   SetWindowClassesDirty(WC_SMALLMAP);
00724   return true;
00725 }
00726 
00727 static bool InvalidateDetailsWindow(int32 p1)
00728 {
00729   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00730   return true;
00731 }
00732 
00733 static bool InvalidateStationBuildWindow(int32 p1)
00734 {
00735   SetWindowDirty(WC_BUILD_STATION, 0);
00736   return true;
00737 }
00738 
00739 static bool InvalidateBuildIndustryWindow(int32 p1)
00740 {
00741   InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00742   return true;
00743 }
00744 
00745 static bool CloseSignalGUI(int32 p1)
00746 {
00747   if (p1 == 0) {
00748     DeleteWindowByClass(WC_BUILD_SIGNAL);
00749   }
00750   return true;
00751 }
00752 
00753 static bool InvalidateTownViewWindow(int32 p1)
00754 {
00755   InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00756   return true;
00757 }
00758 
00759 static bool DeleteSelectStationWindow(int32 p1)
00760 {
00761   DeleteWindowById(WC_SELECT_STATION, 0);
00762   return true;
00763 }
00764 
00765 static bool UpdateConsists(int32 p1)
00766 {
00767   Train *t;
00768   FOR_ALL_TRAINS(t) {
00769     /* Update the consist of all trains so the maximum speed is set correctly. */
00770     if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(true);
00771   }
00772   return true;
00773 }
00774 
00775 /* Check service intervals of vehicles, p1 is value of % or day based servicing */
00776 static bool CheckInterval(int32 p1)
00777 {
00778   VehicleDefaultSettings *vds;
00779   if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00780     vds = &_settings_client.company.vehicle;
00781   } else {
00782     vds = &Company::Get(_current_company)->settings.vehicle;
00783   }
00784 
00785   if (p1) {
00786     vds->servint_trains   = 50;
00787     vds->servint_roadveh  = 50;
00788     vds->servint_aircraft = 50;
00789     vds->servint_ships    = 50;
00790   } else {
00791     vds->servint_trains   = 150;
00792     vds->servint_roadveh  = 150;
00793     vds->servint_aircraft = 100;
00794     vds->servint_ships    = 360;
00795   }
00796 
00797   InvalidateDetailsWindow(0);
00798 
00799   return true;
00800 }
00801 
00802 static bool TrainAccelerationModelChanged(int32 p1)
00803 {
00804   Train *t;
00805   FOR_ALL_TRAINS(t) {
00806     if (t->IsFrontEngine()) {
00807       t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit();
00808       t->UpdateAcceleration();
00809     }
00810   }
00811 
00812   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00813   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00814   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00815   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00816 
00817   return true;
00818 }
00819 
00825 static bool TrainSlopeSteepnessChanged(int32 p1)
00826 {
00827   Train *t;
00828   FOR_ALL_TRAINS(t) {
00829     if (t->IsFrontEngine()) t->CargoChanged();
00830   }
00831 
00832   return true;
00833 }
00834 
00840 static bool RoadVehAccelerationModelChanged(int32 p1)
00841 {
00842   if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00843     RoadVehicle *rv;
00844     FOR_ALL_ROADVEHICLES(rv) {
00845       if (rv->IsRoadVehFront()) {
00846         rv->CargoChanged();
00847       }
00848     }
00849   }
00850 
00851   /* These windows show acceleration values only when realistic acceleration is on. They must be redrawn after a setting change. */
00852   SetWindowClassesDirty(WC_ENGINE_PREVIEW);
00853   InvalidateWindowClassesData(WC_BUILD_VEHICLE, 0);
00854   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00855 
00856   return true;
00857 }
00858 
00864 static bool RoadVehSlopeSteepnessChanged(int32 p1)
00865 {
00866   RoadVehicle *rv;
00867   FOR_ALL_ROADVEHICLES(rv) {
00868     if (rv->IsRoadVehFront()) rv->CargoChanged();
00869   }
00870 
00871   return true;
00872 }
00873 
00874 static bool DragSignalsDensityChanged(int32)
00875 {
00876   InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00877 
00878   return true;
00879 }
00880 
00881 static bool TownFoundingChanged(int32 p1)
00882 {
00883   if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00884     DeleteWindowById(WC_FOUND_TOWN, 0);
00885     return true;
00886   }
00887   InvalidateWindowData(WC_FOUND_TOWN, 0);
00888   return true;
00889 }
00890 
00891 static bool InvalidateVehTimetableWindow(int32 p1)
00892 {
00893   InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00894   return true;
00895 }
00896 
00904 static bool InvalidateNewGRFChangeWindows(int32 p1)
00905 {
00906   InvalidateWindowClassesData(WC_SAVELOAD);
00907   DeleteWindowByClass(WC_GAME_OPTIONS);
00908   ReInitAllWindows();
00909   return true;
00910 }
00911 
00912 static bool InvalidateCompanyLiveryWindow(int32 p1)
00913 {
00914   InvalidateWindowClassesData(WC_COMPANY_COLOUR);
00915   return RedrawScreen(p1);
00916 }
00917 
00918 static bool InvalidateIndustryViewWindow(int32 p1)
00919 {
00920   InvalidateWindowClassesData(WC_INDUSTRY_VIEW);
00921   return true;
00922 }
00923 
00924 /*
00925  * A: competitors
00926  * B: competitor start time. Deprecated since savegame version 110.
00927  * C: town count (3 = high, 0 = very low)
00928  * D: industry count (4 = high, 0 = none)
00929  * E: inital loan (in GBP)
00930  * F: interest rate
00931  * G: running costs (0 = low, 2 = high)
00932  * H: construction speed of competitors (0 = very slow, 4 = very fast)
00933  * I: competitor intelligence. Deprecated since savegame version 110.
00934  * J: breakdowns (0 = off, 2 = normal)
00935  * K: subsidy multiplier (0 = 1.5, 3 = 4.0)
00936  * L: construction cost (0-2)
00937  * M: terrain type (0 = very flat, 3 = mountainous)
00938  * N: amount of water (0 = very low, 3 = high)
00939  * O: economy (0 = steady, 1 = fluctuating)
00940  * P: Train reversing (0 = end of line + stations, 1 = end of line)
00941  * Q: disasters
00942  * R: area restructuring (0 = permissive, 2 = hostile)
00943  * S: the difficulty level
00944  */
00945 static const DifficultySettings _default_game_diff[3] = { /*
00946    A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
00947   {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0}, 
00948   {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1}, 
00949   {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2}, 
00950 };
00951 
00952 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00953 {
00954   assert(mode <= 3);
00955 
00956   if (mode != 3) {
00957     *gm_opt = _default_game_diff[mode];
00958   } else {
00959     gm_opt->diff_level = 3;
00960   }
00961 }
00962 
00964 static void ValidateSettings()
00965 {
00966   /* Force the difficulty levels to correct values if they are invalid. */
00967   if (_settings_newgame.difficulty.diff_level != 3) {
00968     SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00969   }
00970 
00971   /* Do not allow a custom sea level with the original land generator. */
00972   if (_settings_newgame.game_creation.land_generator == 0 &&
00973       _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00974     _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
00975   }
00976 }
00977 
00978 static bool DifficultyReset(int32 level)
00979 {
00980   SetDifficultyLevel(level, (_game_mode == GM_MENU) ? &_settings_newgame.difficulty : &_settings_game.difficulty);
00981   return true;
00982 }
00983 
00984 static bool DifficultyChange(int32)
00985 {
00986   if (_game_mode == GM_MENU) {
00987     if (_settings_newgame.difficulty.diff_level != 3) {
00988       ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, WL_WARNING);
00989       _settings_newgame.difficulty.diff_level = 3;
00990     }
00991     SetWindowClassesDirty(WC_SELECT_GAME);
00992   } else {
00993     _settings_game.difficulty.diff_level = 3;
00994   }
00995 
00996   /* If we are a network-client, update the difficult setting (if it is open).
00997    * Use this instead of just dirtying the window because we need to load in
00998    * the new difficulty settings */
00999   if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
01000     ShowGameDifficulty();
01001   }
01002 
01003   return true;
01004 }
01005 
01006 static bool DifficultyNoiseChange(int32 i)
01007 {
01008   if (_game_mode == GM_NORMAL) {
01009     UpdateAirportsNoise();
01010     if (_settings_game.economy.station_noise_level) {
01011       InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
01012     }
01013   }
01014 
01015   return DifficultyChange(i);
01016 }
01017 
01018 static bool MaxNoAIsChange(int32 i)
01019 {
01020   if (((_game_mode == GM_MENU) ? _settings_newgame.difficulty : _settings_game.difficulty).max_no_competitors != 0 &&
01021 #ifdef ENABLE_AI
01022       AI::GetInfoList()->size() == 0 &&
01023 #endif /* ENABLE_AI */
01024       (!_networking || _network_server)) {
01025     ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
01026   }
01027 
01028   return DifficultyChange(i);
01029 }
01030 
01036 static bool CheckRoadSide(int p1)
01037 {
01038   extern bool RoadVehiclesAreBuilt();
01039   return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
01040 }
01041 
01049 static int32 ConvertLandscape(const char *value)
01050 {
01051   /* try with the old values */
01052   return LookupOneOfMany("normal|hilly|desert|candy", value);
01053 }
01054 
01055 static bool CheckFreeformEdges(int32 p1)
01056 {
01057   if (_game_mode == GM_MENU) return true;
01058   if (p1 != 0) {
01059     Ship *s;
01060     FOR_ALL_SHIPS(s) {
01061       if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
01062         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01063         return false;
01064       }
01065     }
01066     Station *st;
01067     FOR_ALL_STATIONS(st) {
01068       if (TileX(st->xy) == 0 || TileY(st->xy) == 0) {
01069         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR);
01070         return false;
01071       }
01072     }
01073     for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
01074     for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
01075   } else {
01076     for (uint i = 0; i < MapMaxX(); i++) {
01077       if (TileHeight(TileXY(i, 1)) != 0) {
01078         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01079         return false;
01080       }
01081     }
01082     for (uint i = 1; i < MapMaxX(); i++) {
01083       if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
01084         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01085         return false;
01086       }
01087     }
01088     for (uint i = 0; i < MapMaxY(); i++) {
01089       if (TileHeight(TileXY(1, i)) != 0) {
01090         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01091         return false;
01092       }
01093     }
01094     for (uint i = 1; i < MapMaxY(); i++) {
01095       if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
01096         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, WL_ERROR);
01097         return false;
01098       }
01099     }
01100     /* Make tiles at the border water again. */
01101     for (uint i = 0; i < MapMaxX(); i++) {
01102       SetTileHeight(TileXY(i, 0), 0);
01103       SetTileType(TileXY(i, 0), MP_WATER);
01104     }
01105     for (uint i = 0; i < MapMaxY(); i++) {
01106       SetTileHeight(TileXY(0, i), 0);
01107       SetTileType(TileXY(0, i), MP_WATER);
01108     }
01109   }
01110   MarkWholeScreenDirty();
01111   return true;
01112 }
01113 
01118 static bool ChangeDynamicEngines(int32 p1)
01119 {
01120   if (_game_mode == GM_MENU) return true;
01121 
01122   const Vehicle *v;
01123   FOR_ALL_VEHICLES(v) {
01124     if (IsCompanyBuildableVehicleType(v)) {
01125       ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, WL_ERROR);
01126       return false;
01127     }
01128   }
01129 
01130   /* Reset the engines, they will get new EngineIDs */
01131   _engine_mngr.ResetToDefaultMapping();
01132   ReloadNewGRFData();
01133 
01134   return true;
01135 }
01136 
01137 static bool StationCatchmentChanged(int32 p1)
01138 {
01139   Station::RecomputeIndustriesNearForAll();
01140   return true;
01141 }
01142 
01143 #ifdef ENABLE_NETWORK
01144 
01145 static bool UpdateClientName(int32 p1)
01146 {
01147   NetworkUpdateClientName();
01148   return true;
01149 }
01150 
01151 static bool UpdateServerPassword(int32 p1)
01152 {
01153   if (strcmp(_settings_client.network.server_password, "*") == 0) {
01154     _settings_client.network.server_password[0] = '\0';
01155   }
01156 
01157   return true;
01158 }
01159 
01160 static bool UpdateRconPassword(int32 p1)
01161 {
01162   if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01163     _settings_client.network.rcon_password[0] = '\0';
01164   }
01165 
01166   return true;
01167 }
01168 
01169 static bool UpdateClientConfigValues(int32 p1)
01170 {
01171   if (_network_server) NetworkServerSendConfigUpdate();
01172 
01173   return true;
01174 }
01175 
01176 #endif /* ENABLE_NETWORK */
01177 
01178 
01179 /* End - Callback Functions */
01180 
01184 static void PrepareOldDiffCustom()
01185 {
01186   memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01187 }
01188 
01195 static void HandleOldDiffCustom(bool savegame)
01196 {
01197   uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(4)) ? 1 : 0);
01198 
01199   if (!savegame) {
01200     /* If we did read to old_diff_custom, then at least one value must be non 0. */
01201     bool old_diff_custom_used = false;
01202     for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01203       old_diff_custom_used = (_old_diff_custom[i] != 0);
01204     }
01205 
01206     if (!old_diff_custom_used) return;
01207   }
01208 
01209   for (uint i = 0; i < options_to_load; i++) {
01210     const SettingDesc *sd = &_settings[i];
01211     /* Skip deprecated options */
01212     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01213     void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01214     Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01215   }
01216 }
01217 
01224 static bool ConvertOldNewsSetting(const char *name, const char *value)
01225 {
01226   if (strcasecmp(name, "openclose") == 0) {
01227     /* openclose has been split in "open" and "close".
01228      * So the job is now to decrypt the value of the old news config
01229      * and give it to the two newly introduced ones*/
01230 
01231     NewsDisplay display = ND_OFF; // default
01232     if (strcasecmp(value, "full") == 0) {
01233       display = ND_FULL;
01234     } else if (strcasecmp(value, "summarized") == 0) {
01235       display = ND_SUMMARY;
01236     }
01237     /* tranfert of values */
01238     _news_type_data[NT_INDUSTRY_OPEN].display = display;
01239     _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01240     return true;
01241   }
01242   return false;
01243 }
01244 
01250 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01251 {
01252   IniGroup *group = ini->GetGroup(grpname);
01253   IniItem *item;
01254 
01255   /* If no group exists, return */
01256   if (group == NULL) return;
01257 
01258   for (item = group->item; item != NULL; item = item->next) {
01259     int news_item = -1;
01260     for (int i = 0; i < NT_END; i++) {
01261       if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01262         news_item = i;
01263         break;
01264       }
01265     }
01266 
01267     /* the config been read is not within current aceptable config */
01268     if (news_item == -1) {
01269       /* if the conversion function cannot process it, advice by a debug warning*/
01270       if (!ConvertOldNewsSetting(item->name, item->value)) {
01271         DEBUG(misc, 0, "Invalid display option: %s", item->name);
01272       }
01273       /* in all cases, there is nothing left to do */
01274       continue;
01275     }
01276 
01277     if (StrEmpty(item->value)) {
01278       DEBUG(misc, 0, "Empty display value for newstype %s", item->name);
01279       continue;
01280     } else if (strcasecmp(item->value, "full") == 0) {
01281       _news_type_data[news_item].display = ND_FULL;
01282     } else if (strcasecmp(item->value, "off") == 0) {
01283       _news_type_data[news_item].display = ND_OFF;
01284     } else if (strcasecmp(item->value, "summarized") == 0) {
01285       _news_type_data[news_item].display = ND_SUMMARY;
01286     } else {
01287       DEBUG(misc, 0, "Invalid display value for newstype %s: %s", item->name, item->value);
01288       continue;
01289     }
01290   }
01291 }
01292 
01293 static void AILoadConfig(IniFile *ini, const char *grpname)
01294 {
01295 #ifdef ENABLE_AI
01296   IniGroup *group = ini->GetGroup(grpname);
01297   IniItem *item;
01298 
01299   /* Clean any configured AI */
01300   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01301     AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME)->ChangeAI(NULL);
01302   }
01303 
01304   /* If no group exists, return */
01305   if (group == NULL) return;
01306 
01307   CompanyID c = COMPANY_FIRST;
01308   for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01309     AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01310 
01311     config->ChangeAI(item->name);
01312     if (!config->HasAI()) {
01313       if (strcmp(item->name, "none") != 0) {
01314         DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01315         continue;
01316       }
01317     }
01318     if (item->value != NULL) config->StringToSettings(item->value);
01319   }
01320 #endif /* ENABLE_AI */
01321 }
01322 
01329 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01330 {
01331   IniGroup *group = ini->GetGroup(grpname);
01332   IniItem *item;
01333   GRFConfig *first = NULL;
01334   GRFConfig **curr = &first;
01335 
01336   if (group == NULL) return NULL;
01337 
01338   for (item = group->item; item != NULL; item = item->next) {
01339     GRFConfig *c = new GRFConfig(item->name);
01340 
01341     /* Parse parameters */
01342     if (!StrEmpty(item->value)) {
01343       c->num_params = ParseIntList(item->value, (int*)c->param, lengthof(c->param));
01344       if (c->num_params == (byte)-1) {
01345         ShowInfoF("ini: error in array '%s'", item->name);
01346         c->num_params = 0;
01347       }
01348     }
01349 
01350     /* Check if item is valid */
01351     if (!FillGRFDetails(c, is_static)) {
01352       const char *msg;
01353 
01354       if (c->status == GCS_NOT_FOUND) {
01355         msg = "not found";
01356       } else if (HasBit(c->flags, GCF_UNSAFE)) {
01357         msg = "unsafe for static use";
01358       } else if (HasBit(c->flags, GCF_SYSTEM)) {
01359         msg = "system NewGRF";
01360       } else {
01361         msg = "unknown";
01362       }
01363 
01364       ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01365       delete c;
01366       continue;
01367     }
01368 
01369     /* Check for duplicate GRFID (will also check for duplicate filenames) */
01370     bool duplicate = false;
01371     for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01372       if (gc->ident.grfid == c->ident.grfid) {
01373         ShowInfoF("ini: ignoring  NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01374         duplicate = true;
01375         break;
01376       }
01377     }
01378     if (duplicate) {
01379       delete c;
01380       continue;
01381     }
01382 
01383     /* Mark file as static to avoid saving in savegame. */
01384     if (is_static) SetBit(c->flags, GCF_STATIC);
01385 
01386     /* Add item to list */
01387     *curr = c;
01388     curr = &c->next;
01389   }
01390 
01391   return first;
01392 }
01393 
01399 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01400 {
01401   IniGroup *group = ini->GetGroup(grpname);
01402 
01403   for (int i = 0; i < NT_END; i++) {
01404     const char *value;
01405     int v = _news_type_data[i].display;
01406 
01407     value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01408 
01409     group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01410   }
01411 }
01412 
01413 static void AISaveConfig(IniFile *ini, const char *grpname)
01414 {
01415 #ifdef ENABLE_AI
01416   IniGroup *group = ini->GetGroup(grpname);
01417 
01418   if (group == NULL) return;
01419   group->Clear();
01420 
01421   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01422     AIConfig *config = AIConfig::GetConfig(c, AIConfig::AISS_FORCE_NEWGAME);
01423     const char *name;
01424     char value[1024];
01425     config->SettingsToString(value, lengthof(value));
01426 
01427     if (config->HasAI()) {
01428       name = config->GetName();
01429     } else {
01430       name = "none";
01431     }
01432 
01433     IniItem *item = new IniItem(group, name, strlen(name));
01434     item->SetValue(value);
01435   }
01436 #endif /* ENABLE_AI */
01437 }
01438 
01443 static void SaveVersionInConfig(IniFile *ini)
01444 {
01445   IniGroup *group = ini->GetGroup("version");
01446 
01447   char version[9];
01448   snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01449 
01450   const char * const versions[][2] = {
01451     { "version_string", _openttd_revision },
01452     { "version_number", version }
01453   };
01454 
01455   for (uint i = 0; i < lengthof(versions); i++) {
01456     group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01457   }
01458 }
01459 
01460 /* Save a GRF configuration to the given group name */
01461 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01462 {
01463   ini->RemoveGroup(grpname);
01464   IniGroup *group = ini->GetGroup(grpname);
01465   const GRFConfig *c;
01466 
01467   for (c = list; c != NULL; c = c->next) {
01468     char params[512];
01469     GRFBuildParamList(params, c, lastof(params));
01470 
01471     group->GetItem(c->filename, true)->SetValue(params);
01472   }
01473 }
01474 
01475 /* Common handler for saving/loading variables to the configuration file */
01476 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01477 {
01478   proc(ini, (const SettingDesc*)_misc_settings,    "misc",  NULL);
01479   proc(ini, (const SettingDesc*)_music_settings,   "music", &msf);
01480 #if defined(WIN32) && !defined(DEDICATED)
01481   proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
01482 #endif /* WIN32 */
01483 
01484   proc(ini, _settings,         "patches",  &_settings_newgame);
01485   proc(ini, _currency_settings,"currency", &_custom_currency);
01486   proc(ini, _company_settings, "company",  &_settings_client.company);
01487 
01488 #ifdef ENABLE_NETWORK
01489   proc_list(ini, "server_bind_addresses", &_network_bind_list);
01490   proc_list(ini, "servers", &_network_host_list);
01491   proc_list(ini, "bans",    &_network_ban_list);
01492 #endif /* ENABLE_NETWORK */
01493 }
01494 
01495 static IniFile *IniLoadConfig()
01496 {
01497   IniFile *ini = new IniFile(_list_group_names);
01498   ini->LoadFromDisk(_config_file);
01499   return ini;
01500 }
01501 
01503 void LoadFromConfig()
01504 {
01505   IniFile *ini = IniLoadConfig();
01506   ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
01507 
01508   HandleSettingDescs(ini, IniLoadSettings, IniLoadSettingList);
01509   _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01510   _grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
01511   NewsDisplayLoadConfig(ini, "news_display");
01512   AILoadConfig(ini, "ai_players");
01513 
01514   PrepareOldDiffCustom();
01515   IniLoadSettings(ini, _gameopt_settings, "gameopt", &_settings_newgame);
01516   HandleOldDiffCustom(false);
01517 
01518   ValidateSettings();
01519   delete ini;
01520 }
01521 
01523 void SaveToConfig()
01524 {
01525   IniFile *ini = IniLoadConfig();
01526 
01527   /* Remove some obsolete groups. These have all been loaded into other groups. */
01528   ini->RemoveGroup("patches");
01529   ini->RemoveGroup("yapf");
01530   ini->RemoveGroup("gameopt");
01531 
01532   HandleSettingDescs(ini, IniSaveSettings, IniSaveSettingList);
01533   GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01534   GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01535   NewsDisplaySaveConfig(ini, "news_display");
01536   AISaveConfig(ini, "ai_players");
01537   SaveVersionInConfig(ini);
01538   ini->SaveToDisk(_config_file);
01539   delete ini;
01540 }
01541 
01546 void GetGRFPresetList(GRFPresetList *list)
01547 {
01548   list->Clear();
01549 
01550   IniFile *ini = IniLoadConfig();
01551   IniGroup *group;
01552   for (group = ini->group; group != NULL; group = group->next) {
01553     if (strncmp(group->name, "preset-", 7) == 0) {
01554       *list->Append() = strdup(group->name + 7);
01555     }
01556   }
01557 
01558   delete ini;
01559 }
01560 
01567 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01568 {
01569   char *section = (char*)alloca(strlen(config_name) + 8);
01570   sprintf(section, "preset-%s", config_name);
01571 
01572   IniFile *ini = IniLoadConfig();
01573   GRFConfig *config = GRFLoadConfig(ini, section, false);
01574   delete ini;
01575 
01576   return config;
01577 }
01578 
01585 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01586 {
01587   char *section = (char*)alloca(strlen(config_name) + 8);
01588   sprintf(section, "preset-%s", config_name);
01589 
01590   IniFile *ini = IniLoadConfig();
01591   GRFSaveConfig(ini, section, config);
01592   ini->SaveToDisk(_config_file);
01593   delete ini;
01594 }
01595 
01600 void DeleteGRFPresetFromConfig(const char *config_name)
01601 {
01602   char *section = (char*)alloca(strlen(config_name) + 8);
01603   sprintf(section, "preset-%s", config_name);
01604 
01605   IniFile *ini = IniLoadConfig();
01606   ini->RemoveGroup(section);
01607   ini->SaveToDisk(_config_file);
01608   delete ini;
01609 }
01610 
01611 static const SettingDesc *GetSettingDescription(uint index)
01612 {
01613   if (index >= lengthof(_settings)) return NULL;
01614   return &_settings[index];
01615 }
01616 
01628 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01629 {
01630   const SettingDesc *sd = GetSettingDescription(p1);
01631 
01632   if (sd == NULL) return CMD_ERROR;
01633   if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01634 
01635   if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01636   if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01637   if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01638       (_game_mode == GM_NORMAL ||
01639       (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01640     return CMD_ERROR;
01641   }
01642 
01643   if (flags & DC_EXEC) {
01644     GameSettings *s = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game;
01645     void *var = GetVariableAddress(s, &sd->save);
01646 
01647     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01648     int32 newval = (int32)p2;
01649 
01650     Write_ValidateSetting(var, sd, newval);
01651     newval = (int32)ReadValue(var, sd->save.conv);
01652 
01653     if (oldval == newval) return CommandCost();
01654 
01655     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01656       WriteValue(var, sd->save.conv, (int64)oldval);
01657       return CommandCost();
01658     }
01659 
01660     if (sd->desc.flags & SGF_NO_NETWORK) {
01661       GamelogStartAction(GLAT_SETTING);
01662       GamelogSetting(sd->desc.name, oldval, newval);
01663       GamelogStopAction();
01664     }
01665 
01666     SetWindowDirty(WC_GAME_OPTIONS, 0);
01667   }
01668 
01669   return CommandCost();
01670 }
01671 
01682 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01683 {
01684   if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01685   const SettingDesc *sd = &_company_settings[p1];
01686 
01687   if (flags & DC_EXEC) {
01688     void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01689 
01690     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01691     int32 newval = (int32)p2;
01692 
01693     Write_ValidateSetting(var, sd, newval);
01694     newval = (int32)ReadValue(var, sd->save.conv);
01695 
01696     if (oldval == newval) return CommandCost();
01697 
01698     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01699       WriteValue(var, sd->save.conv, (int64)oldval);
01700       return CommandCost();
01701     }
01702 
01703     SetWindowDirty(WC_GAME_OPTIONS, 0);
01704   }
01705 
01706   return CommandCost();
01707 }
01708 
01716 bool SetSettingValue(uint index, int32 value, bool force_newgame)
01717 {
01718   const SettingDesc *sd = &_settings[index];
01719   /* If an item is company-based, we do not send it over the network
01720    * (if any) to change. Also *hack*hack* we update the _newgame version
01721    * of settings because changing a company-based setting in a game also
01722    * changes its defaults. At least that is the convention we have chosen */
01723   if (sd->save.conv & SLF_NETWORK_NO) {
01724     void *var = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01725     Write_ValidateSetting(var, sd, value);
01726 
01727     if (_game_mode != GM_MENU) {
01728       void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01729       Write_ValidateSetting(var2, sd, value);
01730     }
01731     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01732     SetWindowDirty(WC_GAME_OPTIONS, 0);
01733     return true;
01734   }
01735 
01736   if (force_newgame) {
01737     void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01738     Write_ValidateSetting(var2, sd, value);
01739     return true;
01740   }
01741 
01742   /* send non-company-based settings over the network */
01743   if (!_networking || (_networking && _network_server)) {
01744     return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01745   }
01746   return false;
01747 }
01748 
01755 void SetCompanySetting(uint index, int32 value)
01756 {
01757   const SettingDesc *sd = &_company_settings[index];
01758   if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01759     DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01760   } else {
01761     void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01762     Write_ValidateSetting(var, sd, value);
01763     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01764   }
01765 }
01766 
01770 void SetDefaultCompanySettings(CompanyID cid)
01771 {
01772   Company *c = Company::Get(cid);
01773   const SettingDesc *sd;
01774   for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01775     void *var = GetVariableAddress(&c->settings, &sd->save);
01776     Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01777   }
01778 }
01779 
01780 #if defined(ENABLE_NETWORK)
01781 
01784 void SyncCompanySettings()
01785 {
01786   const SettingDesc *sd;
01787   uint i = 0;
01788   for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01789     const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01790     const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01791     uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01792     uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01793     if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL, _local_company);
01794   }
01795 }
01796 #endif /* ENABLE_NETWORK */
01797 
01803 uint GetCompanySettingIndex(const char *name)
01804 {
01805   uint i;
01806   const SettingDesc *sd = GetSettingFromName(name, &i);
01807   assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01808   return i;
01809 }
01810 
01818 bool SetSettingValue(uint index, const char *value, bool force_newgame)
01819 {
01820   const SettingDesc *sd = &_settings[index];
01821   assert(sd->save.conv & SLF_NETWORK_NO);
01822 
01823   if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
01824     char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01825     free(*var);
01826     *var = strcmp(value, "(null)") == 0 ? NULL : strdup(value);
01827   } else {
01828     char *var = (char*)GetVariableAddress(NULL, &sd->save);
01829     ttd_strlcpy(var, value, sd->save.length);
01830   }
01831   if (sd->desc.proc != NULL) sd->desc.proc(0);
01832 
01833   return true;
01834 }
01835 
01843 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01844 {
01845   const SettingDesc *sd;
01846 
01847   /* First check all full names */
01848   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01849     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01850     if (strcmp(sd->desc.name, name) == 0) return sd;
01851   }
01852 
01853   /* Then check the shortcut variant of the name. */
01854   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01855     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01856     const char *short_name = strchr(sd->desc.name, '.');
01857     if (short_name != NULL) {
01858       short_name++;
01859       if (strcmp(short_name, name) == 0) return sd;
01860     }
01861   }
01862 
01863   if (strncmp(name, "company.", 8) == 0) name += 8;
01864   /* And finally the company-based settings */
01865   for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01866     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01867     if (strcmp(sd->desc.name, name) == 0) return sd;
01868   }
01869 
01870   return NULL;
01871 }
01872 
01873 /* Those 2 functions need to be here, else we have to make some stuff non-static
01874  * and besides, it is also better to keep stuff like this at the same place */
01875 void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
01876 {
01877   uint index;
01878   const SettingDesc *sd = GetSettingFromName(name, &index);
01879 
01880   if (sd == NULL) {
01881     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01882     return;
01883   }
01884 
01885   bool success;
01886   if (sd->desc.cmd == SDT_STRING) {
01887     success = SetSettingValue(index, value, force_newgame);
01888   } else {
01889     uint32 val;
01890     extern bool GetArgumentInteger(uint32 *value, const char *arg);
01891     success = GetArgumentInteger(&val, value);
01892     if (!success) {
01893       IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01894       return;
01895     }
01896 
01897     success = SetSettingValue(index, val, force_newgame);
01898   }
01899 
01900   if (!success) {
01901     if (_network_server) {
01902       IConsoleError("This command/variable is not available during network games.");
01903     } else {
01904       IConsoleError("This command/variable is only available to a network server.");
01905     }
01906   }
01907 }
01908 
01909 void IConsoleSetSetting(const char *name, int value)
01910 {
01911   uint index;
01912   const SettingDesc *sd = GetSettingFromName(name, &index);
01913   assert(sd != NULL);
01914   SetSettingValue(index, value);
01915 }
01916 
01922 void IConsoleGetSetting(const char *name, bool force_newgame)
01923 {
01924   char value[20];
01925   uint index;
01926   const SettingDesc *sd = GetSettingFromName(name, &index);
01927   const void *ptr;
01928 
01929   if (sd == NULL) {
01930     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01931     return;
01932   }
01933 
01934   ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
01935 
01936   if (sd->desc.cmd == SDT_STRING) {
01937     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01938   } else {
01939     if (sd->desc.cmd == SDT_BOOLX) {
01940       snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01941     } else {
01942       snprintf(value, sizeof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01943     }
01944 
01945     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %u)",
01946       name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01947   }
01948 }
01949 
01955 void IConsoleListSettings(const char *prefilter)
01956 {
01957   IConsolePrintF(CC_WARNING, "All settings with their current value:");
01958 
01959   for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01960     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01961     if (prefilter != NULL && strstr(sd->desc.name, prefilter) == NULL) continue;
01962     char value[80];
01963     const void *ptr = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01964 
01965     if (sd->desc.cmd == SDT_BOOLX) {
01966       snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01967     } else if (sd->desc.cmd == SDT_STRING) {
01968       snprintf(value, sizeof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char **)ptr : (const char *)ptr);
01969     } else {
01970       snprintf(value, lengthof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
01971     }
01972     IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01973   }
01974 
01975   IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01976 }
01977 
01984 static void LoadSettings(const SettingDesc *osd, void *object)
01985 {
01986   for (; osd->save.cmd != SL_END; osd++) {
01987     const SaveLoad *sld = &osd->save;
01988     void *ptr = GetVariableAddress(object, sld);
01989 
01990     if (!SlObjectMember(ptr, sld)) continue;
01991     if (IsNumericType(sld->conv)) Write_ValidateSetting(ptr, osd, ReadValue(ptr, sld->conv));
01992   }
01993 }
01994 
02001 static void SaveSettings(const SettingDesc *sd, void *object)
02002 {
02003   /* We need to write the CH_RIFF header, but unfortunately can't call
02004    * SlCalcLength() because we have a different format. So do this manually */
02005   const SettingDesc *i;
02006   size_t length = 0;
02007   for (i = sd; i->save.cmd != SL_END; i++) {
02008     length += SlCalcObjMemberLength(object, &i->save);
02009   }
02010   SlSetLength(length);
02011 
02012   for (i = sd; i->save.cmd != SL_END; i++) {
02013     void *ptr = GetVariableAddress(object, &i->save);
02014     SlObjectMember(ptr, &i->save);
02015   }
02016 }
02017 
02018 static void Load_OPTS()
02019 {
02020   /* Copy over default setting since some might not get loaded in
02021    * a networking environment. This ensures for example that the local
02022    * autosave-frequency stays when joining a network-server */
02023   PrepareOldDiffCustom();
02024   LoadSettings(_gameopt_settings, &_settings_game);
02025   HandleOldDiffCustom(true);
02026 }
02027 
02028 static void Load_PATS()
02029 {
02030   /* Copy over default setting since some might not get loaded in
02031    * a networking environment. This ensures for example that the local
02032    * signal_side stays when joining a network-server */
02033   LoadSettings(_settings, &_settings_game);
02034 }
02035 
02036 static void Check_PATS()
02037 {
02038   LoadSettings(_settings, &_load_check_data.settings);
02039 }
02040 
02041 static void Save_PATS()
02042 {
02043   SaveSettings(_settings, &_settings_game);
02044 }
02045 
02046 void CheckConfig()
02047 {
02048   /*
02049    * Increase old default values for pf_maxdepth and pf_maxlength
02050    * to support big networks.
02051    */
02052   if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
02053     _settings_newgame.pf.opf.pf_maxdepth = 48;
02054     _settings_newgame.pf.opf.pf_maxlength = 4096;
02055   }
02056 }
02057 
02058 extern const ChunkHandler _setting_chunk_handlers[] = {
02059   { 'OPTS', NULL,      Load_OPTS, NULL, NULL,       CH_RIFF},
02060   { 'PATS', Save_PATS, Load_PATS, NULL, Check_PATS, CH_RIFF | CH_LAST},
02061 };
02062 
02063 static bool IsSignedVarMemType(VarType vt)
02064 {
02065   switch (GetVarMemType(vt)) {
02066     case SLE_VAR_I8:
02067     case SLE_VAR_I16:
02068     case SLE_VAR_I32:
02069     case SLE_VAR_I64:
02070       return true;
02071   }
02072   return false;
02073 }

Generated on Fri Dec 31 17:15:38 2010 for OpenTTD by  doxygen 1.6.1