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