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

Generated on Sun May 8 07:30:18 2011 for OpenTTD by  doxygen 1.6.1