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 
00025 #include "stdafx.h"
00026 #include "currency.h"
00027 #include "screenshot.h"
00028 #include "variables.h"
00029 #include "network/network.h"
00030 #include "network/network_func.h"
00031 #include "settings_internal.h"
00032 #include "command_func.h"
00033 #include "console_func.h"
00034 #include "pathfinder/pathfinder_type.h"
00035 #include "genworld.h"
00036 #include "train.h"
00037 #include "news_func.h"
00038 #include "window_func.h"
00039 #include "strings_func.h"
00040 #include "vehicle_func.h"
00041 #include "sound_func.h"
00042 #include "company_func.h"
00043 #include "rev.h"
00044 #ifdef WITH_FREETYPE
00045 #include "fontcache.h"
00046 #endif
00047 #include "textbuf_gui.h"
00048 #include "rail_gui.h"
00049 #include "elrail_func.h"
00050 #include "gui.h"
00051 #include "town.h"
00052 #include "video/video_driver.hpp"
00053 #include "sound/sound_driver.hpp"
00054 #include "music/music_driver.hpp"
00055 #include "blitter/factory.hpp"
00056 #include "base_media_base.h"
00057 #include "gamelog.h"
00058 #include "settings_func.h"
00059 #include "ini_type.h"
00060 #include "ai/ai.hpp"
00061 #include "ai/ai_config.hpp"
00062 #include "newgrf.h"
00063 #include "ship.h"
00064 
00065 #include "void_map.h"
00066 #include "station_base.h"
00067 #include "infrastructure_func.h"
00068 
00069 #include "table/strings.h"
00070 #include "table/settings.h"
00071 
00072 ClientSettings _settings_client;
00073 GameSettings _settings_game;
00074 GameSettings _settings_newgame;
00075 VehicleDefaultSettings _old_vds; 
00076 
00077 typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
00078 typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
00079 
00080 static bool IsSignedVarMemType(VarType vt);
00081 
00085 static const char * const _list_group_names[] = {
00086   "bans",
00087   "newgrf",
00088   "servers",
00089   "server_bind_addresses",
00090   NULL
00091 };
00092 
00098 static int lookup_oneofmany(const char *many, const char *one, size_t onelen = 0)
00099 {
00100   const char *s;
00101   int idx;
00102 
00103   if (onelen == 0) onelen = strlen(one);
00104 
00105   /* check if it's an integer */
00106   if (*one >= '0' && *one <= '9')
00107     return strtoul(one, NULL, 0);
00108 
00109   idx = 0;
00110   for (;;) {
00111     /* find end of item */
00112     s = many;
00113     while (*s != '|' && *s != 0) s++;
00114     if ((size_t)(s - many) == onelen && !memcmp(one, many, onelen)) return idx;
00115     if (*s == 0) return -1;
00116     many = s + 1;
00117     idx++;
00118   }
00119 }
00120 
00126 static uint32 lookup_manyofmany(const char *many, const char *str)
00127 {
00128   const char *s;
00129   int r;
00130   uint32 res = 0;
00131 
00132   for (;;) {
00133     /* skip "whitespace" */
00134     while (*str == ' ' || *str == '\t' || *str == '|') str++;
00135     if (*str == 0) break;
00136 
00137     s = str;
00138     while (*s != 0 && *s != ' ' && *s != '\t' && *s != '|') s++;
00139 
00140     r = lookup_oneofmany(many, str, s - str);
00141     if (r == -1) return (uint32)-1;
00142 
00143     SetBit(res, r); // value found, set it
00144     if (*s == 0) break;
00145     str = s + 1;
00146   }
00147   return res;
00148 }
00149 
00156 static int parse_intlist(const char *p, int *items, int maxitems)
00157 {
00158   int n = 0, v;
00159   char *end;
00160 
00161   for (;;) {
00162     v = strtol(p, &end, 0);
00163     if (p == end || n == maxitems) return -1;
00164     p = end;
00165     items[n++] = v;
00166     if (*p == '\0') break;
00167     if (*p != ',' && *p != ' ') return -1;
00168     p++;
00169   }
00170 
00171   return n;
00172 }
00173 
00180 static bool load_intlist(const char *str, void *array, int nelems, VarType type)
00181 {
00182   int items[64];
00183   int i, nitems;
00184 
00185   if (str == NULL) {
00186     memset(items, 0, sizeof(items));
00187     nitems = nelems;
00188   } else {
00189     nitems = parse_intlist(str, items, lengthof(items));
00190     if (nitems != nelems) return false;
00191   }
00192 
00193   switch (type) {
00194   case SLE_VAR_BL:
00195   case SLE_VAR_I8:
00196   case SLE_VAR_U8:
00197     for (i = 0; i != nitems; i++) ((byte*)array)[i] = items[i];
00198     break;
00199   case SLE_VAR_I16:
00200   case SLE_VAR_U16:
00201     for (i = 0; i != nitems; i++) ((uint16*)array)[i] = items[i];
00202     break;
00203   case SLE_VAR_I32:
00204   case SLE_VAR_U32:
00205     for (i = 0; i != nitems; i++) ((uint32*)array)[i] = items[i];
00206     break;
00207   default: NOT_REACHED();
00208   }
00209 
00210   return true;
00211 }
00212 
00220 static void make_intlist(char *buf, const char *last, const void *array, int nelems, VarType type)
00221 {
00222   int i, v = 0;
00223   byte *p = (byte*)array;
00224 
00225   for (i = 0; i != nelems; i++) {
00226     switch (type) {
00227     case SLE_VAR_BL:
00228     case SLE_VAR_I8:  v = *(int8*)p;   p += 1; break;
00229     case SLE_VAR_U8:  v = *(byte*)p;   p += 1; break;
00230     case SLE_VAR_I16: v = *(int16*)p;  p += 2; break;
00231     case SLE_VAR_U16: v = *(uint16*)p; p += 2; break;
00232     case SLE_VAR_I32: v = *(int32*)p;  p += 4; break;
00233     case SLE_VAR_U32: v = *(uint32*)p; p += 4; break;
00234     default: NOT_REACHED();
00235     }
00236     buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
00237   }
00238 }
00239 
00245 static void make_oneofmany(char *buf, const char *last, const char *many, int id)
00246 {
00247   int orig_id = id;
00248 
00249   /* Look for the id'th element */
00250   while (--id >= 0) {
00251     for (; *many != '|'; many++) {
00252       if (*many == '\0') { // not found
00253         seprintf(buf, last, "%d", orig_id);
00254         return;
00255       }
00256     }
00257     many++; // pass the |-character
00258   }
00259 
00260   /* copy string until next item (|) or the end of the list if this is the last one */
00261   while (*many != '\0' && *many != '|' && buf < last) *buf++ = *many++;
00262   *buf = '\0';
00263 }
00264 
00271 static void make_manyofmany(char *buf, const char *last, const char *many, uint32 x)
00272 {
00273   const char *start;
00274   int i = 0;
00275   bool init = true;
00276 
00277   for (; x != 0; x >>= 1, i++) {
00278     start = many;
00279     while (*many != 0 && *many != '|') many++; // advance to the next element
00280 
00281     if (HasBit(x, 0)) { // item found, copy it
00282       if (!init) buf += seprintf(buf, last, "|");
00283       init = false;
00284       if (start == many) {
00285         buf += seprintf(buf, last, "%d", i);
00286       } else {
00287         memcpy(buf, start, many - start);
00288         buf += many - start;
00289       }
00290     }
00291 
00292     if (*many == '|') many++;
00293   }
00294 
00295   *buf = '\0';
00296 }
00297 
00302 static const void *string_to_val(const SettingDescBase *desc, const char *orig_str)
00303 {
00304   const char *str = orig_str == NULL ? "" : orig_str;
00305   switch (desc->cmd) {
00306   case SDT_NUMX: {
00307     char *end;
00308     unsigned long val = strtoul(str, &end, 0);
00309     if (*end != '\0') ShowInfoF("ini: trailing characters at end of setting '%s'", desc->name);
00310     return (void*)val;
00311   }
00312   case SDT_ONEOFMANY: {
00313     long r = lookup_oneofmany(desc->many, str);
00314     /* if the first attempt of conversion from string to the appropriate value fails,
00315      * look if we have defined a converter from old value to new value. */
00316     if (r == -1 && desc->proc_cnvt != NULL) r = desc->proc_cnvt(str);
00317     if (r != -1) return (void*)r; // and here goes converted value
00318     ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name); // sorry, we failed
00319     return 0;
00320   }
00321   case SDT_MANYOFMANY: {
00322     unsigned long r = lookup_manyofmany(desc->many, str);
00323     if (r != (unsigned long)-1) return (void*)r;
00324     ShowInfoF("ini: invalid value '%s' for '%s'", str, desc->name);
00325     return 0;
00326   }
00327   case SDT_BOOLX:
00328     if (strcmp(str, "true")  == 0 || strcmp(str, "on")  == 0 || strcmp(str, "1") == 0)
00329       return (void*)true;
00330     if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0)
00331       return (void*)false;
00332     ShowInfoF("ini: invalid setting value '%s' for '%s'", str, desc->name);
00333     break;
00334 
00335   case SDT_STRING: return orig_str;
00336   case SDT_INTLIST: return str;
00337   default: break;
00338   }
00339 
00340   return NULL;
00341 }
00342 
00350 static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
00351 {
00352   const SettingDescBase *sdb = &sd->desc;
00353 
00354   if (sdb->cmd != SDT_BOOLX &&
00355       sdb->cmd != SDT_NUMX &&
00356       sdb->cmd != SDT_ONEOFMANY &&
00357       sdb->cmd != SDT_MANYOFMANY) {
00358     return;
00359   }
00360 
00361   /* We cannot know the maximum value of a bitset variable, so just have faith */
00362   if (sdb->cmd != SDT_MANYOFMANY) {
00363     /* We need to take special care of the uint32 type as we receive from the function
00364      * a signed integer. While here also bail out on 64-bit settings as those are not
00365      * supported. Unsigned 8 and 16-bit variables are safe since they fit into a signed
00366      * 32-bit variable
00367      * TODO: Support 64-bit settings/variables */
00368     switch (GetVarMemType(sd->save.conv)) {
00369       case SLE_VAR_NULL: return;
00370       case SLE_VAR_BL:
00371       case SLE_VAR_I8:
00372       case SLE_VAR_U8:
00373       case SLE_VAR_I16:
00374       case SLE_VAR_U16:
00375       case SLE_VAR_I32: {
00376         /* Override the minimum value. No value below sdb->min, except special value 0 */
00377         if (!(sdb->flags & SGF_0ISDISABLED) || val != 0) val = Clamp(val, sdb->min, sdb->max);
00378       } break;
00379       case SLE_VAR_U32: {
00380         /* Override the minimum value. No value below sdb->min, except special value 0 */
00381         uint min = ((sdb->flags & SGF_0ISDISABLED) && (uint)val <= (uint)sdb->min) ? 0 : sdb->min;
00382         WriteValue(ptr, SLE_VAR_U32, (int64)ClampU(val, min, sdb->max));
00383         return;
00384       }
00385       case SLE_VAR_I64:
00386       case SLE_VAR_U64:
00387       default: NOT_REACHED();
00388     }
00389   }
00390 
00391   WriteValue(ptr, sd->save.conv, (int64)val);
00392 }
00393 
00400 static void ini_load_settings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00401 {
00402   IniGroup *group;
00403   IniGroup *group_def = ini->GetGroup(grpname);
00404   IniItem *item;
00405   const void *p;
00406   void *ptr;
00407   const char *s;
00408 
00409   for (; sd->save.cmd != SL_END; sd++) {
00410     const SettingDescBase *sdb = &sd->desc;
00411     const SaveLoad        *sld = &sd->save;
00412 
00413     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00414 
00415     /* For settings.xx.yy load the settings from [xx] yy = ? */
00416     s = strchr(sdb->name, '.');
00417     if (s != NULL) {
00418       group = ini->GetGroup(sdb->name, s - sdb->name);
00419       s++;
00420     } else {
00421       s = sdb->name;
00422       group = group_def;
00423     }
00424 
00425     item = group->GetItem(s, false);
00426     if (item == NULL && group != group_def) {
00427       /* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
00428        * did not exist (e.g. loading old config files with a [settings] section */
00429       item = group_def->GetItem(s, false);
00430     }
00431     if (item == NULL) {
00432       /* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
00433        * did not exist (e.g. loading old config files with a [yapf] section */
00434       const char *sc = strchr(s, '.');
00435       if (sc != NULL) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
00436     }
00437 
00438     p = (item == NULL) ? sdb->def : string_to_val(sdb, item->value);
00439     ptr = GetVariableAddress(object, sld);
00440 
00441     switch (sdb->cmd) {
00442     case SDT_BOOLX: // All four are various types of (integer) numbers
00443     case SDT_NUMX:
00444     case SDT_ONEOFMANY:
00445     case SDT_MANYOFMANY:
00446       Write_ValidateSetting(ptr, sd, (int32)(size_t)p); break;
00447 
00448     case SDT_STRING:
00449       switch (GetVarMemType(sld->conv)) {
00450         case SLE_VAR_STRB:
00451         case SLE_VAR_STRBQ:
00452           if (p != NULL) ttd_strlcpy((char*)ptr, (const char*)p, sld->length);
00453           break;
00454         case SLE_VAR_STR:
00455         case SLE_VAR_STRQ:
00456           free(*(char**)ptr);
00457           *(char**)ptr = p == NULL ? NULL : strdup((const char*)p);
00458           break;
00459         case SLE_VAR_CHAR: if (p != NULL) *(char*)ptr = *(char*)p; break;
00460         default: NOT_REACHED();
00461       }
00462       break;
00463 
00464     case SDT_INTLIST: {
00465       if (!load_intlist((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
00466         ShowInfoF("ini: error in array '%s'", sdb->name);
00467       } else if (sd->desc.proc_cnvt != NULL) {
00468         sd->desc.proc_cnvt((const char*)p);
00469       }
00470       break;
00471     }
00472     default: NOT_REACHED();
00473     }
00474   }
00475 }
00476 
00488 static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *grpname, void *object)
00489 {
00490   IniGroup *group_def = NULL, *group;
00491   IniItem *item;
00492   char buf[512];
00493   const char *s;
00494   void *ptr;
00495 
00496   for (; sd->save.cmd != SL_END; sd++) {
00497     const SettingDescBase *sdb = &sd->desc;
00498     const SaveLoad        *sld = &sd->save;
00499 
00500     /* If the setting is not saved to the configuration
00501      * file, just continue with the next setting */
00502     if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
00503     if (sld->conv & SLF_CONFIG_NO) continue;
00504 
00505     /* XXX - wtf is this?? (group override?) */
00506     s = strchr(sdb->name, '.');
00507     if (s != NULL) {
00508       group = ini->GetGroup(sdb->name, s - sdb->name);
00509       s++;
00510     } else {
00511       if (group_def == NULL) group_def = ini->GetGroup(grpname);
00512       s = sdb->name;
00513       group = group_def;
00514     }
00515 
00516     item = group->GetItem(s, true);
00517     ptr = GetVariableAddress(object, sld);
00518 
00519     if (item->value != NULL) {
00520       /* check if the value is the same as the old value */
00521       const void *p = string_to_val(sdb, item->value);
00522 
00523       /* The main type of a variable/setting is in bytes 8-15
00524        * The subtype (what kind of numbers do we have there) is in 0-7 */
00525       switch (sdb->cmd) {
00526       case SDT_BOOLX:
00527       case SDT_NUMX:
00528       case SDT_ONEOFMANY:
00529       case SDT_MANYOFMANY:
00530         switch (GetVarMemType(sld->conv)) {
00531         case SLE_VAR_BL:
00532           if (*(bool*)ptr == (p != NULL)) continue;
00533           break;
00534         case SLE_VAR_I8:
00535         case SLE_VAR_U8:
00536           if (*(byte*)ptr == (byte)(unsigned long)p) continue;
00537           break;
00538         case SLE_VAR_I16:
00539         case SLE_VAR_U16:
00540           if (*(uint16*)ptr == (uint16)(unsigned long)p) continue;
00541           break;
00542         case SLE_VAR_I32:
00543         case SLE_VAR_U32:
00544           if (*(uint32*)ptr == (uint32)(unsigned long)p) continue;
00545           break;
00546         default: NOT_REACHED();
00547         }
00548         break;
00549       default: break; // Assume the other types are always changed
00550       }
00551     }
00552 
00553     /* Value has changed, get the new value and put it into a buffer */
00554     switch (sdb->cmd) {
00555     case SDT_BOOLX:
00556     case SDT_NUMX:
00557     case SDT_ONEOFMANY:
00558     case SDT_MANYOFMANY: {
00559       uint32 i = (uint32)ReadValue(ptr, sld->conv);
00560 
00561       switch (sdb->cmd) {
00562       case SDT_BOOLX:      strecpy(buf, (i != 0) ? "true" : "false", lastof(buf)); break;
00563       case SDT_NUMX:       seprintf(buf, lastof(buf), IsSignedVarMemType(sld->conv) ? "%d" : "%u", i); break;
00564       case SDT_ONEOFMANY:  make_oneofmany(buf, lastof(buf), sdb->many, i); break;
00565       case SDT_MANYOFMANY: make_manyofmany(buf, lastof(buf), sdb->many, i); break;
00566       default: NOT_REACHED();
00567       }
00568     } break;
00569 
00570     case SDT_STRING:
00571       switch (GetVarMemType(sld->conv)) {
00572       case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
00573       case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
00574       case SLE_VAR_STR:  strecpy(buf, *(char**)ptr, lastof(buf)); break;
00575       case SLE_VAR_STRQ:
00576         if (*(char**)ptr == NULL) {
00577           buf[0] = '\0';
00578         } else {
00579           seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
00580         }
00581         break;
00582       case SLE_VAR_CHAR: buf[0] = *(char*)ptr; buf[1] = '\0'; break;
00583       default: NOT_REACHED();
00584       }
00585       break;
00586 
00587     case SDT_INTLIST:
00588       make_intlist(buf, lastof(buf), ptr, sld->length, GetVarMemType(sld->conv));
00589       break;
00590     default: NOT_REACHED();
00591     }
00592 
00593     /* The value is different, that means we have to write it to the ini */
00594     free(item->value);
00595     item->value = strdup(buf);
00596   }
00597 }
00598 
00607 static void ini_load_setting_list(IniFile *ini, const char *grpname, StringList *list)
00608 {
00609   IniGroup *group = ini->GetGroup(grpname);
00610 
00611   if (group == NULL || list == NULL) return;
00612 
00613   list->Clear();
00614 
00615   for (const IniItem *item = group->item; item != NULL; item = item->next) {
00616     if (item->name != NULL) *list->Append() = strdup(item->name);
00617   }
00618 }
00619 
00628 static void ini_save_setting_list(IniFile *ini, const char *grpname, StringList *list)
00629 {
00630   IniGroup *group = ini->GetGroup(grpname);
00631 
00632   if (group == NULL || list == NULL) return;
00633   group->Clear();
00634 
00635   for (char **iter = list->Begin(); iter != list->End(); iter++) {
00636     group->GetItem(*iter, true)->SetValue("");
00637   }
00638 }
00639 
00640 /* Begin - Callback Functions for the various settings
00641  * virtual PositionMainToolbar function, calls the right one.*/
00642 static bool v_PositionMainToolbar(int32 p1)
00643 {
00644   if (_game_mode != GM_MENU) PositionMainToolbar(NULL);
00645   return true;
00646 }
00647 
00648 static bool PopulationInLabelActive(int32 p1)
00649 {
00650   UpdateAllTownVirtCoords();
00651   return true;
00652 }
00653 
00654 static bool RedrawScreen(int32 p1)
00655 {
00656   MarkWholeScreenDirty();
00657   return true;
00658 }
00659 
00660 static bool InvalidateDetailsWindow(int32 p1)
00661 {
00662   SetWindowClassesDirty(WC_VEHICLE_DETAILS);
00663   return true;
00664 }
00665 
00666 static bool InvalidateStationBuildWindow(int32 p1)
00667 {
00668   SetWindowDirty(WC_BUILD_STATION, 0);
00669   return true;
00670 }
00671 
00672 static bool InvalidateBuildIndustryWindow(int32 p1)
00673 {
00674   InvalidateWindowData(WC_BUILD_INDUSTRY, 0);
00675   return true;
00676 }
00677 
00678 static bool CloseSignalGUI(int32 p1)
00679 {
00680   if (p1 == 0) {
00681     DeleteWindowByClass(WC_BUILD_SIGNAL);
00682   }
00683   return true;
00684 }
00685 
00686 static bool InvalidateTownViewWindow(int32 p1)
00687 {
00688   InvalidateWindowClassesData(WC_TOWN_VIEW, p1);
00689   return true;
00690 }
00691 
00692 static bool DeleteSelectStationWindow(int32 p1)
00693 {
00694   DeleteWindowById(WC_SELECT_STATION, 0);
00695   return true;
00696 }
00697 
00698 static bool UpdateConsists(int32 p1)
00699 {
00700   Train *t;
00701   FOR_ALL_TRAINS(t) {
00702     /* Update the consist of all trains so the maximum speed is set correctly. */
00703     if (t->IsFrontEngine() || t->IsFreeWagon()) TrainConsistChanged(t, true);
00704   }
00705   return true;
00706 }
00707 
00708 /* Check service intervals of vehicles, p1 is value of % or day based servicing */
00709 static bool CheckInterval(int32 p1)
00710 {
00711   VehicleDefaultSettings *vds;
00712   if (_game_mode == GM_MENU || !Company::IsValidID(_current_company)) {
00713     vds = &_settings_client.company.vehicle;
00714   } else {
00715     vds = &Company::Get(_current_company)->settings.vehicle;
00716   }
00717 
00718   if (p1) {
00719     vds->servint_trains   = 50;
00720     vds->servint_roadveh  = 50;
00721     vds->servint_aircraft = 50;
00722     vds->servint_ships    = 50;
00723   } else {
00724     vds->servint_trains   = 150;
00725     vds->servint_roadveh  = 150;
00726     vds->servint_aircraft = 360;
00727     vds->servint_ships    = 100;
00728   }
00729 
00730   InvalidateDetailsWindow(0);
00731 
00732   return true;
00733 }
00734 
00735 static bool TrainAccelerationModelChanged(int32 p1)
00736 {
00737   Train *t;
00738   FOR_ALL_TRAINS(t) {
00739     if (t->IsFrontEngine()) {
00740       t->tcache.cached_max_curve_speed = GetTrainCurveSpeedLimit(t);
00741       UpdateTrainAcceleration(t);
00742     }
00743   }
00744 
00745   return true;
00746 }
00747 
00748 static bool DragSignalsDensityChanged(int32)
00749 {
00750   InvalidateWindowData(WC_BUILD_SIGNAL, 0);
00751 
00752   return true;
00753 }
00754 
00755 static bool TownFoundingChanged(int32 p1)
00756 {
00757   if (_game_mode != GM_EDITOR && _settings_game.economy.found_town == TF_FORBIDDEN) {
00758     DeleteWindowById(WC_FOUND_TOWN, 0);
00759     return true;
00760   }
00761   InvalidateWindowData(WC_FOUND_TOWN, 0);
00762   return true;
00763 }
00764 
00765 static bool InvalidateVehTimetableWindow(int32 p1)
00766 {
00767   InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, -2);
00768   return true;
00769 }
00770 
00771 /*
00772  * A: competitors
00773  * B: competitor start time. Deprecated since savegame version 110.
00774  * C: town count (3 = high, 0 = very low)
00775  * D: industry count (4 = high, 0 = none)
00776  * E: inital loan (in GBP)
00777  * F: interest rate
00778  * G: running costs (0 = low, 2 = high)
00779  * H: construction speed of competitors (0 = very slow, 4 = very fast)
00780  * I: competitor intelligence. Deprecated since savegame version 110.
00781  * J: breakdowns (0 = off, 2 = normal)
00782  * K: subsidy multiplier (0 = 1.5, 3 = 4.0)
00783  * L: construction cost (0-2)
00784  * M: terrain type (0 = very flat, 3 = mountainous)
00785  * N: amount of water (0 = very low, 3 = high)
00786  * O: economy (0 = steady, 1 = fluctuating)
00787  * P: Train reversing (0 = end of line + stations, 1 = end of line)
00788  * Q: disasters
00789  * R: area restructuring (0 = permissive, 2 = hostile)
00790  * S: the difficulty level
00791  */
00792 static const DifficultySettings _default_game_diff[3] = { /*
00793    A, C, D,      E, F, G, H, J, K, L, M, N, O, P, Q, R, S*/
00794   {2, 2, 4, 300000, 2, 0, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0}, 
00795   {4, 2, 3, 150000, 3, 1, 3, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1}, 
00796   {7, 3, 3, 100000, 4, 1, 3, 2, 0, 2, 3, 2, 1, 1, 1, 2, 2}, 
00797 };
00798 
00799 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt)
00800 {
00801   assert(mode <= 3);
00802 
00803   if (mode != 3) {
00804     *gm_opt = _default_game_diff[mode];
00805   } else {
00806     gm_opt->diff_level = 3;
00807   }
00808 }
00809 
00814 static void CheckDifficultyLevels()
00815 {
00816   if (_settings_newgame.difficulty.diff_level != 3) {
00817     SetDifficultyLevel(_settings_newgame.difficulty.diff_level, &_settings_newgame.difficulty);
00818   }
00819 }
00820 
00821 static bool DifficultyReset(int32 level)
00822 {
00823   SetDifficultyLevel(level, (_game_mode == GM_MENU) ? &_settings_newgame.difficulty : &_settings_game.difficulty);
00824   return true;
00825 }
00826 
00827 static bool DifficultyChange(int32)
00828 {
00829   if (_game_mode == GM_MENU) {
00830     if (_settings_newgame.difficulty.diff_level != 3) {
00831       ShowErrorMessage(STR_WARNING_DIFFICULTY_TO_CUSTOM, INVALID_STRING_ID, 0, 0);
00832       _settings_newgame.difficulty.diff_level = 3;
00833     }
00834     SetWindowClassesDirty(WC_SELECT_GAME);
00835   } else {
00836     _settings_game.difficulty.diff_level = 3;
00837   }
00838 
00839   /* If we are a network-client, update the difficult setting (if it is open).
00840    * Use this instead of just dirtying the window because we need to load in
00841    * the new difficulty settings */
00842   if (_networking && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) {
00843     ShowGameDifficulty();
00844   }
00845 
00846   return true;
00847 }
00848 
00849 static bool DifficultyNoiseChange(int32 i)
00850 {
00851   if (_game_mode == GM_NORMAL) {
00852     UpdateAirportsNoise();
00853     if (_settings_game.economy.station_noise_level) {
00854       InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
00855     }
00856   }
00857 
00858   return DifficultyChange(i);
00859 }
00860 
00866 static bool CheckRoadSide(int p1)
00867 {
00868   extern bool RoadVehiclesAreBuilt();
00869   return _game_mode == GM_MENU || !RoadVehiclesAreBuilt();
00870 }
00871 
00878 static int32 ConvertLandscape(const char *value)
00879 {
00880   /* try with the old values */
00881   return lookup_oneofmany("normal|hilly|desert|candy", value);
00882 }
00883 
00890 static int32 CheckNoiseToleranceLevel(const char *value)
00891 {
00892   GameSettings *s = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game;
00893   for (uint16 i = 0; i < lengthof(s->economy.town_noise_population); i++) {
00894     s->economy.town_noise_population[i] = max(uint16(200 * (i + 1)), s->economy.town_noise_population[i]);
00895   }
00896   return 0;
00897 }
00898 
00899 static bool CheckFreeformEdges(int32 p1)
00900 {
00901   if (_game_mode == GM_MENU) return true;
00902   if (p1 != 0) {
00903     Ship *s;
00904     FOR_ALL_SHIPS(s) {
00905       if (TileX(s->tile) == 0 || TileY(s->tile) == 0) {
00906         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, 0, 0);
00907         return false;
00908       }
00909     }
00910     Station *st;
00911     FOR_ALL_STATIONS(st) {
00912       if (TileX(st->xy) == 0 || TileY(st->xy) == 0) {
00913         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, 0, 0);
00914         return false;
00915       }
00916     }
00917     for (uint i = 0; i < MapSizeX(); i++) MakeVoid(TileXY(i, 0));
00918     for (uint i = 0; i < MapSizeY(); i++) MakeVoid(TileXY(0, i));
00919   } else {
00920     for (uint i = 0; i < MapMaxX(); i++) {
00921       if (TileHeight(TileXY(i, 1)) != 0) {
00922         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00923         return false;
00924       }
00925     }
00926     for (uint i = 1; i < MapMaxX(); i++) {
00927       if (!IsTileType(TileXY(i, MapMaxY() - 1), MP_WATER) || TileHeight(TileXY(1, MapMaxY())) != 0) {
00928         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00929         return false;
00930       }
00931     }
00932     for (uint i = 0; i < MapMaxY(); i++) {
00933       if (TileHeight(TileXY(1, i)) != 0) {
00934         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00935         return false;
00936       }
00937     }
00938     for (uint i = 1; i < MapMaxY(); i++) {
00939       if (!IsTileType(TileXY(MapMaxX() - 1, i), MP_WATER) || TileHeight(TileXY(MapMaxX(), i)) != 0) {
00940         ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_WATER, INVALID_STRING_ID, 0, 0);
00941         return false;
00942       }
00943     }
00944     /* Make tiles at the border water again. */
00945     for (uint i = 0; i < MapMaxX(); i++) {
00946       SetTileHeight(TileXY(i, 0), 0);
00947       SetTileType(TileXY(i, 0), MP_WATER);
00948     }
00949     for (uint i = 0; i < MapMaxY(); i++) {
00950       SetTileHeight(TileXY(0, i), 0);
00951       SetTileType(TileXY(0, i), MP_WATER);
00952     }
00953   }
00954   MarkWholeScreenDirty();
00955   return true;
00956 }
00957 
00962 static bool ChangeDynamicEngines(int32 p1)
00963 {
00964   if (_game_mode == GM_MENU) return true;
00965 
00966   const Vehicle *v;
00967   FOR_ALL_VEHICLES(v) {
00968     if (IsCompanyBuildableVehicleType(v)) {
00969       ShowErrorMessage(STR_CONFIG_SETTING_DYNAMIC_ENGINES_EXISTING_VEHICLES, INVALID_STRING_ID, 0, 0);
00970       return false;
00971     }
00972   }
00973 
00974   /* Reset the engines, they will get new EngineIDs */
00975   _engine_mngr.ResetToDefaultMapping();
00976   ReloadNewGRFData();
00977 
00978   return true;
00979 }
00980 
00981 static bool StationCatchmentChanged(int32 p1)
00982 {
00983   Station::RecomputeIndustriesNearForAll();
00984   return true;
00985 }
00986 
00987 static bool CheckSharingRail(int32 p1)
00988 {
00989   if (!CheckSharingChangePossible(VEH_TRAIN)) return false;
00990   UpdateAllBlockSignals();
00991   return true;
00992 }
00993 
00994 static bool CheckSharingRoad(int32 p1)
00995 {
00996   return CheckSharingChangePossible(VEH_ROAD);
00997 }
00998 
00999 static bool CheckSharingWater(int32 p1)
01000 {
01001   return CheckSharingChangePossible(VEH_SHIP);
01002 }
01003 
01004 static bool CheckSharingAir(int32 p1)
01005 {
01006   return CheckSharingChangePossible(VEH_AIRCRAFT);
01007 }
01008 
01009 #ifdef ENABLE_NETWORK
01010 
01011 static bool UpdateClientName(int32 p1)
01012 {
01013   NetworkUpdateClientName();
01014   return true;
01015 }
01016 
01017 static bool UpdateServerPassword(int32 p1)
01018 {
01019   if (strcmp(_settings_client.network.server_password, "*") == 0) {
01020     _settings_client.network.server_password[0] = '\0';
01021   }
01022 
01023   return true;
01024 }
01025 
01026 static bool UpdateRconPassword(int32 p1)
01027 {
01028   if (strcmp(_settings_client.network.rcon_password, "*") == 0) {
01029     _settings_client.network.rcon_password[0] = '\0';
01030   }
01031 
01032   return true;
01033 }
01034 
01035 static bool UpdateClientConfigValues(int32 p1)
01036 {
01037   if (_network_server) NetworkServerSendConfigUpdate();
01038 
01039   return true;
01040 }
01041 
01042 #endif /* ENABLE_NETWORK */
01043 
01044 
01045 /* End - Callback Functions */
01046 
01050 static void PrepareOldDiffCustom()
01051 {
01052   memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
01053 }
01054 
01061 static void HandleOldDiffCustom(bool savegame)
01062 {
01063   uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && CheckSavegameVersion(4)) ? 1 : 0);
01064 
01065   if (!savegame) {
01066     /* If we did read to old_diff_custom, then at least one value must be non 0. */
01067     bool old_diff_custom_used = false;
01068     for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
01069       old_diff_custom_used = (_old_diff_custom[i] != 0);
01070     }
01071 
01072     if (!old_diff_custom_used) return;
01073   }
01074 
01075   for (uint i = 0; i < options_to_load; i++) {
01076     const SettingDesc *sd = &_settings[i];
01077     /* Skip deprecated options */
01078     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01079     void *var = GetVariableAddress(savegame ? &_settings_game : &_settings_newgame, &sd->save);
01080     Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
01081   }
01082 }
01083 
01088 static bool ConvertOldNewsSetting(const char *name, const char *value)
01089 {
01090   if (strcasecmp(name, "openclose") == 0) {
01091     /* openclose has been split in "open" and "close".
01092      * So the job is now to decrypt the value of the old news config
01093      * and give it to the two newly introduced ones*/
01094 
01095     NewsDisplay display = ND_OFF; // default
01096     if (strcasecmp(value, "full") == 0) {
01097       display = ND_FULL;
01098     } else if (strcasecmp(value, "summarized") == 0) {
01099       display = ND_SUMMARY;
01100     }
01101     /* tranfert of values */
01102     _news_type_data[NT_INDUSTRY_OPEN].display = display;
01103     _news_type_data[NT_INDUSTRY_CLOSE].display = display;
01104     return true;
01105   }
01106   return false;
01107 }
01108 
01109 static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
01110 {
01111   IniGroup *group = ini->GetGroup(grpname);
01112   IniItem *item;
01113 
01114   /* If no group exists, return */
01115   if (group == NULL) return;
01116 
01117   for (item = group->item; item != NULL; item = item->next) {
01118     int news_item = -1;
01119     for (int i = 0; i < NT_END; i++) {
01120       if (strcasecmp(item->name, _news_type_data[i].name) == 0) {
01121         news_item = i;
01122         break;
01123       }
01124     }
01125 
01126     /* the config been read is not within current aceptable config */
01127     if (news_item == -1) {
01128       /* if the conversion function cannot process it, advice by a debug warning*/
01129       if (!ConvertOldNewsSetting(item->name, item->value)) {
01130         DEBUG(misc, 0, "Invalid display option: %s", item->name);
01131       }
01132       /* in all cases, there is nothing left to do */
01133       continue;
01134     }
01135 
01136     if (strcasecmp(item->value, "full") == 0) {
01137       _news_type_data[news_item].display = ND_FULL;
01138     } else if (strcasecmp(item->value, "off") == 0) {
01139       _news_type_data[news_item].display = ND_OFF;
01140     } else if (strcasecmp(item->value, "summarized") == 0) {
01141       _news_type_data[news_item].display = ND_SUMMARY;
01142     } else {
01143       DEBUG(misc, 0, "Invalid display value: %s", item->value);
01144       continue;
01145     }
01146   }
01147 }
01148 
01149 static void AILoadConfig(IniFile *ini, const char *grpname)
01150 {
01151   IniGroup *group = ini->GetGroup(grpname);
01152   IniItem *item;
01153 
01154   /* Clean any configured AI */
01155   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01156     AIConfig::GetConfig(c, true)->ChangeAI(NULL);
01157   }
01158 
01159   /* If no group exists, return */
01160   if (group == NULL) return;
01161 
01162   CompanyID c = COMPANY_FIRST;
01163   for (item = group->item; c < MAX_COMPANIES && item != NULL; c++, item = item->next) {
01164     AIConfig *config = AIConfig::GetConfig(c, true);
01165 
01166     config->ChangeAI(item->name);
01167     if (!config->HasAI()) {
01168       if (strcmp(item->name, "none") != 0) {
01169         DEBUG(ai, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
01170         continue;
01171       }
01172     }
01173     if (item->value != NULL) config->StringToSettings(item->value);
01174   }
01175 }
01176 
01177 /* Load a GRF configuration from the given group name */
01178 static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_static)
01179 {
01180   IniGroup *group = ini->GetGroup(grpname);
01181   IniItem *item;
01182   GRFConfig *first = NULL;
01183   GRFConfig **curr = &first;
01184 
01185   if (group == NULL) return NULL;
01186 
01187   for (item = group->item; item != NULL; item = item->next) {
01188     GRFConfig *c = CallocT<GRFConfig>(1);
01189     c->filename = strdup(item->name);
01190 
01191     /* Parse parameters */
01192     if (!StrEmpty(item->value)) {
01193       c->num_params = parse_intlist(item->value, (int*)c->param, lengthof(c->param));
01194       if (c->num_params == (byte)-1) {
01195         ShowInfoF("ini: error in array '%s'", item->name);
01196         c->num_params = 0;
01197       }
01198     }
01199 
01200     /* Check if item is valid */
01201     if (!FillGRFDetails(c, is_static)) {
01202       const char *msg;
01203 
01204       if (c->status == GCS_NOT_FOUND) {
01205         msg = "not found";
01206       } else if (HasBit(c->flags, GCF_UNSAFE)) {
01207         msg = "unsafe for static use";
01208       } else if (HasBit(c->flags, GCF_SYSTEM)) {
01209         msg = "system NewGRF";
01210       } else {
01211         msg = "unknown";
01212       }
01213 
01214       ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
01215       ClearGRFConfig(&c);
01216       continue;
01217     }
01218 
01219     /* Check for duplicate GRFID (will also check for duplicate filenames) */
01220     bool duplicate = false;
01221     for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
01222       if (gc->grfid == c->grfid) {
01223         ShowInfoF("ini: ignoring  NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
01224         duplicate = true;
01225         break;
01226       }
01227     }
01228     if (duplicate) {
01229       ClearGRFConfig(&c);
01230       continue;
01231     }
01232 
01233     /* Mark file as static to avoid saving in savegame. */
01234     if (is_static) SetBit(c->flags, GCF_STATIC);
01235 
01236     /* Add item to list */
01237     *curr = c;
01238     curr = &c->next;
01239   }
01240 
01241   return first;
01242 }
01243 
01244 static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
01245 {
01246   IniGroup *group = ini->GetGroup(grpname);
01247 
01248   for (int i = 0; i < NT_END; i++) {
01249     const char *value;
01250     int v = _news_type_data[i].display;
01251 
01252     value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
01253 
01254     group->GetItem(_news_type_data[i].name, true)->SetValue(value);
01255   }
01256 }
01257 
01258 static void AISaveConfig(IniFile *ini, const char *grpname)
01259 {
01260   IniGroup *group = ini->GetGroup(grpname);
01261 
01262   if (group == NULL) return;
01263   group->Clear();
01264 
01265   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
01266     AIConfig *config = AIConfig::GetConfig(c, true);
01267     const char *name;
01268     char value[1024];
01269     config->SettingsToString(value, lengthof(value));
01270 
01271     if (config->HasAI()) {
01272       name = config->GetName();
01273     } else {
01274       name = "none";
01275     }
01276 
01277     IniItem *item = new IniItem(group, name, strlen(name));
01278     item->SetValue(value);
01279   }
01280 }
01281 
01286 static void SaveVersionInConfig(IniFile *ini)
01287 {
01288   IniGroup *group = ini->GetGroup("version");
01289 
01290   char version[9];
01291   snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
01292 
01293   const char * const versions[][2] = {
01294     { "version_string", _openttd_revision },
01295     { "version_number", version }
01296   };
01297 
01298   for (uint i = 0; i < lengthof(versions); i++) {
01299     group->GetItem(versions[i][0], true)->SetValue(versions[i][1]);
01300   }
01301 }
01302 
01303 /* Save a GRF configuration to the given group name */
01304 static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
01305 {
01306   ini->RemoveGroup(grpname);
01307   IniGroup *group = ini->GetGroup(grpname);
01308   const GRFConfig *c;
01309 
01310   for (c = list; c != NULL; c = c->next) {
01311     char params[512];
01312     GRFBuildParamList(params, c, lastof(params));
01313 
01314     group->GetItem(c->filename, true)->SetValue(params);
01315   }
01316 }
01317 
01318 /* Common handler for saving/loading variables to the configuration file */
01319 static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
01320 {
01321   proc(ini, (const SettingDesc*)_misc_settings,    "misc",  NULL);
01322   proc(ini, (const SettingDesc*)_music_settings,   "music", &msf);
01323 #if defined(WIN32) && !defined(DEDICATED)
01324   proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
01325 #endif /* WIN32 */
01326 
01327   proc(ini, _settings,         "patches",  &_settings_newgame);
01328   proc(ini, _currency_settings,"currency", &_custom_currency);
01329   proc(ini, _company_settings, "company",  &_settings_client.company);
01330 
01331 #ifdef ENABLE_NETWORK
01332   proc_list(ini, "server_bind_addresses", &_network_bind_list);
01333   proc_list(ini, "servers", &_network_host_list);
01334   proc_list(ini, "bans",    &_network_ban_list);
01335 #endif /* ENABLE_NETWORK */
01336 }
01337 
01338 static IniFile *IniLoadConfig()
01339 {
01340   IniFile *ini = new IniFile(_list_group_names);
01341   ini->LoadFromDisk(_config_file);
01342   return ini;
01343 }
01344 
01346 void LoadFromConfig()
01347 {
01348   IniFile *ini = IniLoadConfig();
01349   ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
01350 
01351   HandleSettingDescs(ini, ini_load_settings, ini_load_setting_list);
01352   _grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
01353   _grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
01354   NewsDisplayLoadConfig(ini, "news_display");
01355   AILoadConfig(ini, "ai_players");
01356 
01357   PrepareOldDiffCustom();
01358   ini_load_settings(ini, _gameopt_settings, "gameopt",  &_settings_newgame);
01359   HandleOldDiffCustom(false);
01360 
01361   CheckDifficultyLevels();
01362   delete ini;
01363 }
01364 
01366 void SaveToConfig()
01367 {
01368   IniFile *ini = IniLoadConfig();
01369 
01370   /* Remove some obsolete groups. These have all been loaded into other groups. */
01371   ini->RemoveGroup("patches");
01372   ini->RemoveGroup("yapf");
01373   ini->RemoveGroup("gameopt");
01374 
01375   HandleSettingDescs(ini, ini_save_settings, ini_save_setting_list);
01376   GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
01377   GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
01378   NewsDisplaySaveConfig(ini, "news_display");
01379   AISaveConfig(ini, "ai_players");
01380   SaveVersionInConfig(ini);
01381   ini->SaveToDisk(_config_file);
01382   delete ini;
01383 }
01384 
01385 void GetGRFPresetList(GRFPresetList *list)
01386 {
01387   list->Clear();
01388 
01389   IniFile *ini = IniLoadConfig();
01390   IniGroup *group;
01391   for (group = ini->group; group != NULL; group = group->next) {
01392     if (strncmp(group->name, "preset-", 7) == 0) {
01393       *list->Append() = strdup(group->name + 7);
01394     }
01395   }
01396 
01397   delete ini;
01398 }
01399 
01400 GRFConfig *LoadGRFPresetFromConfig(const char *config_name)
01401 {
01402   char *section = (char*)alloca(strlen(config_name) + 8);
01403   sprintf(section, "preset-%s", config_name);
01404 
01405   IniFile *ini = IniLoadConfig();
01406   GRFConfig *config = GRFLoadConfig(ini, section, false);
01407   delete ini;
01408 
01409   return config;
01410 }
01411 
01412 void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
01413 {
01414   char *section = (char*)alloca(strlen(config_name) + 8);
01415   sprintf(section, "preset-%s", config_name);
01416 
01417   IniFile *ini = IniLoadConfig();
01418   GRFSaveConfig(ini, section, config);
01419   ini->SaveToDisk(_config_file);
01420   delete ini;
01421 }
01422 
01423 void DeleteGRFPresetFromConfig(const char *config_name)
01424 {
01425   char *section = (char*)alloca(strlen(config_name) + 8);
01426   sprintf(section, "preset-%s", config_name);
01427 
01428   IniFile *ini = IniLoadConfig();
01429   ini->RemoveGroup(section);
01430   ini->SaveToDisk(_config_file);
01431   delete ini;
01432 }
01433 
01434 static const SettingDesc *GetSettingDescription(uint index)
01435 {
01436   if (index >= lengthof(_settings)) return NULL;
01437   return &_settings[index];
01438 }
01439 
01450 CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01451 {
01452   const SettingDesc *sd = GetSettingDescription(p1);
01453 
01454   if (sd == NULL) return CMD_ERROR;
01455   if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
01456 
01457   if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking && _game_mode != GM_MENU) return CMD_ERROR;
01458   if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return CMD_ERROR;
01459   if ((sd->desc.flags & SGF_NEWGAME_ONLY) &&
01460       (_game_mode == GM_NORMAL ||
01461       (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0))) {
01462     return CMD_ERROR;
01463   }
01464 
01465   if (flags & DC_EXEC) {
01466     GameSettings *s = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game;
01467     void *var = GetVariableAddress(s, &sd->save);
01468 
01469     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01470     int32 newval = (int32)p2;
01471 
01472     Write_ValidateSetting(var, sd, newval);
01473     newval = (int32)ReadValue(var, sd->save.conv);
01474 
01475     if (oldval == newval) return CommandCost();
01476 
01477     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01478       WriteValue(var, sd->save.conv, (int64)oldval);
01479       return CommandCost();
01480     }
01481 
01482     if (sd->desc.flags & SGF_NO_NETWORK) {
01483       GamelogStartAction(GLAT_SETTING);
01484       GamelogSetting(sd->desc.name, oldval, newval);
01485       GamelogStopAction();
01486     }
01487 
01488     SetWindowDirty(WC_GAME_OPTIONS, 0);
01489   }
01490 
01491   return CommandCost();
01492 }
01493 
01503 CommandCost CmdChangeCompanySetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01504 {
01505   if (p1 >= lengthof(_company_settings)) return CMD_ERROR;
01506   const SettingDesc *sd = &_company_settings[p1];
01507 
01508   if (flags & DC_EXEC) {
01509     void *var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01510 
01511     int32 oldval = (int32)ReadValue(var, sd->save.conv);
01512     int32 newval = (int32)p2;
01513 
01514     Write_ValidateSetting(var, sd, newval);
01515     newval = (int32)ReadValue(var, sd->save.conv);
01516 
01517     if (oldval == newval) return CommandCost();
01518 
01519     if (sd->desc.proc != NULL && !sd->desc.proc(newval)) {
01520       WriteValue(var, sd->save.conv, (int64)oldval);
01521       return CommandCost();
01522     }
01523 
01524     SetWindowDirty(WC_GAME_OPTIONS, 0);
01525   }
01526 
01527   return CommandCost();
01528 }
01529 
01535 bool SetSettingValue(uint index, int32 value)
01536 {
01537   const SettingDesc *sd = &_settings[index];
01538   /* If an item is company-based, we do not send it over the network
01539    * (if any) to change. Also *hack*hack* we update the _newgame version
01540    * of settings because changing a company-based setting in a game also
01541    * changes its defaults. At least that is the convention we have chosen */
01542   if (sd->save.conv & SLF_NETWORK_NO) {
01543     void *var = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01544     Write_ValidateSetting(var, sd, value);
01545 
01546     if (_game_mode != GM_MENU) {
01547       void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
01548       Write_ValidateSetting(var2, sd, value);
01549     }
01550     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01551     SetWindowDirty(WC_GAME_OPTIONS, 0);
01552     return true;
01553   }
01554 
01555   /* send non-company-based settings over the network */
01556   if (!_networking || (_networking && _network_server)) {
01557     return DoCommandP(0, index, value, CMD_CHANGE_SETTING);
01558   }
01559   return false;
01560 }
01561 
01567 void SetCompanySetting(uint index, int32 value)
01568 {
01569   const SettingDesc *sd = &_company_settings[index];
01570   if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01571     DoCommandP(0, index, value, CMD_CHANGE_COMPANY_SETTING);
01572   } else {
01573     void *var = GetVariableAddress(&_settings_client.company, &sd->save);
01574     Write_ValidateSetting(var, sd, value);
01575     if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv));
01576   }
01577 }
01578 
01582 void SetDefaultCompanySettings(CompanyID cid)
01583 {
01584   Company *c = Company::Get(cid);
01585   const SettingDesc *sd;
01586   for (sd = _company_settings; sd->save.cmd != SL_END; sd++) {
01587     void *var = GetVariableAddress(&c->settings, &sd->save);
01588     Write_ValidateSetting(var, sd, (int32)(size_t)sd->desc.def);
01589   }
01590 }
01591 
01592 #if defined(ENABLE_NETWORK)
01593 
01596 void SyncCompanySettings()
01597 {
01598   const SettingDesc *sd;
01599   uint i = 0;
01600   for (sd = _company_settings; sd->save.cmd != SL_END; sd++, i++) {
01601     const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
01602     const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
01603     uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
01604     uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
01605     if (old_value != new_value) NetworkSend_Command(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, NULL, NULL);
01606   }
01607 }
01608 #endif /* ENABLE_NETWORK */
01609 
01615 uint GetCompanySettingIndex(const char *name)
01616 {
01617   uint i;
01618   const SettingDesc *sd = GetSettingFromName(name, &i);
01619   assert(sd != NULL && (sd->desc.flags & SGF_PER_COMPANY) != 0);
01620   return i;
01621 }
01622 
01629 bool SetSettingValue(uint index, const char *value)
01630 {
01631   const SettingDesc *sd = &_settings[index];
01632   assert(sd->save.conv & SLF_NETWORK_NO);
01633 
01634   char *var = (char*)GetVariableAddress(NULL, &sd->save);
01635   ttd_strlcpy(var, value, sd->save.length);
01636   if (sd->desc.proc != NULL) sd->desc.proc(0);
01637 
01638   return true;
01639 }
01640 
01648 const SettingDesc *GetSettingFromName(const char *name, uint *i)
01649 {
01650   const SettingDesc *sd;
01651 
01652   /* First check all full names */
01653   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01654     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01655     if (strcmp(sd->desc.name, name) == 0) return sd;
01656   }
01657 
01658   /* Then check the shortcut variant of the name. */
01659   for (*i = 0, sd = _settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01660     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01661     const char *short_name = strchr(sd->desc.name, '.');
01662     if (short_name != NULL) {
01663       short_name++;
01664       if (strcmp(short_name, name) == 0) return sd;
01665     }
01666   }
01667 
01668   if (strncmp(name, "company.", 8) == 0) name += 8;
01669   /* And finally the company-based settings */
01670   for (*i = 0, sd = _company_settings; sd->save.cmd != SL_END; sd++, (*i)++) {
01671     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01672     if (strcmp(sd->desc.name, name) == 0) return sd;
01673   }
01674 
01675   return NULL;
01676 }
01677 
01678 /* Those 2 functions need to be here, else we have to make some stuff non-static
01679  * and besides, it is also better to keep stuff like this at the same place */
01680 void IConsoleSetSetting(const char *name, const char *value)
01681 {
01682   uint index;
01683   const SettingDesc *sd = GetSettingFromName(name, &index);
01684 
01685   if (sd == NULL) {
01686     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01687     return;
01688   }
01689 
01690   bool success;
01691   if (sd->desc.cmd == SDT_STRING) {
01692     success = SetSettingValue(index, value);
01693   } else {
01694     uint32 val;
01695     extern bool GetArgumentInteger(uint32 *value, const char *arg);
01696     success = GetArgumentInteger(&val, value);
01697     if (!success) {
01698       IConsolePrintF(CC_ERROR, "'%s' is not an integer.", value);
01699       return;
01700     }
01701 
01702     success = SetSettingValue(index, val);
01703   }
01704 
01705   if (!success) {
01706     if (_network_server) {
01707       IConsoleError("This command/variable is not available during network games.");
01708     } else {
01709       IConsoleError("This command/variable is only available to a network server.");
01710     }
01711   }
01712 }
01713 
01714 void IConsoleSetSetting(const char *name, int value)
01715 {
01716   uint index;
01717   const SettingDesc *sd = GetSettingFromName(name, &index);
01718   assert(sd != NULL);
01719   SetSettingValue(index, value);
01720 }
01721 
01726 void IConsoleGetSetting(const char *name)
01727 {
01728   char value[20];
01729   uint index;
01730   const SettingDesc *sd = GetSettingFromName(name, &index);
01731   const void *ptr;
01732 
01733   if (sd == NULL) {
01734     IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
01735     return;
01736   }
01737 
01738   ptr = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01739 
01740   if (sd->desc.cmd == SDT_STRING) {
01741     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (const char *)ptr);
01742   } else {
01743     if (sd->desc.cmd == SDT_BOOLX) {
01744       snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
01745     } else {
01746       snprintf(value, sizeof(value), "%d", (int32)ReadValue(ptr, sd->save.conv));
01747     }
01748 
01749     IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s' (min: %s%d, max: %d)",
01750       name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
01751   }
01752 }
01753 
01759 void IConsoleListSettings(const char *prefilter)
01760 {
01761   IConsolePrintF(CC_WARNING, "All settings with their current value:");
01762 
01763   for (const SettingDesc *sd = _settings; sd->save.cmd != SL_END; sd++) {
01764     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
01765     if (prefilter != NULL) {
01766       if (strncmp(sd->desc.name, prefilter, min(strlen(sd->desc.name), strlen(prefilter))) != 0) continue;
01767     }
01768     char value[80];
01769     const void *ptr = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game, &sd->save);
01770 
01771     if (sd->desc.cmd == SDT_BOOLX) {
01772       snprintf(value, lengthof(value), (*(bool*)ptr == 1) ? "on" : "off");
01773     } else if (sd->desc.cmd == SDT_STRING) {
01774       snprintf(value, sizeof(value), "%s", (const char *)ptr);
01775     } else {
01776       snprintf(value, lengthof(value), "%d", (uint32)ReadValue(ptr, sd->save.conv));
01777     }
01778     IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
01779   }
01780 
01781   IConsolePrintF(CC_WARNING, "Use 'setting' command to change a value");
01782 }
01783 
01788 static void LoadSettings(const SettingDesc *osd, void *object)
01789 {
01790   for (; osd->save.cmd != SL_END; osd++) {
01791     const SaveLoad *sld = &osd->save;
01792     void *ptr = GetVariableAddress(object, sld);
01793 
01794     if (!SlObjectMember(ptr, sld)) continue;
01795   }
01796 }
01797 
01802 static inline void LoadSettingsGlobList(const SettingDescGlobVarList *sdg)
01803 {
01804   LoadSettings((const SettingDesc*)sdg, NULL);
01805 }
01806 
01811 static void SaveSettings(const SettingDesc *sd, void *object)
01812 {
01813   /* We need to write the CH_RIFF header, but unfortunately can't call
01814    * SlCalcLength() because we have a different format. So do this manually */
01815   const SettingDesc *i;
01816   size_t length = 0;
01817   for (i = sd; i->save.cmd != SL_END; i++) {
01818     length += SlCalcObjMemberLength(object, &i->save);
01819   }
01820   SlSetLength(length);
01821 
01822   for (i = sd; i->save.cmd != SL_END; i++) {
01823     void *ptr = GetVariableAddress(object, &i->save);
01824     SlObjectMember(ptr, &i->save);
01825   }
01826 }
01827 
01831 static inline void SaveSettingsGlobList(const SettingDescGlobVarList *sdg)
01832 {
01833   SaveSettings((const SettingDesc*)sdg, NULL);
01834 }
01835 
01836 static void Load_OPTS()
01837 {
01838   /* Copy over default setting since some might not get loaded in
01839    * a networking environment. This ensures for example that the local
01840    * autosave-frequency stays when joining a network-server */
01841   PrepareOldDiffCustom();
01842   LoadSettings(_gameopt_settings, &_settings_game);
01843   HandleOldDiffCustom(true);
01844 }
01845 
01846 static void Load_PATS()
01847 {
01848   /* Copy over default setting since some might not get loaded in
01849    * a networking environment. This ensures for example that the local
01850    * signal_side stays when joining a network-server */
01851   LoadSettings(_settings, &_settings_game);
01852 }
01853 
01854 static void Save_PATS()
01855 {
01856   SaveSettings(_settings, &_settings_game);
01857 }
01858 
01859 void CheckConfig()
01860 {
01861   /*
01862    * Increase old default values for pf_maxdepth and pf_maxlength
01863    * to support big networks.
01864    */
01865   if (_settings_newgame.pf.opf.pf_maxdepth == 16 && _settings_newgame.pf.opf.pf_maxlength == 512) {
01866     _settings_newgame.pf.opf.pf_maxdepth = 48;
01867     _settings_newgame.pf.opf.pf_maxlength = 4096;
01868   }
01869 }
01870 
01871 extern const ChunkHandler _setting_chunk_handlers[] = {
01872   { 'OPTS', NULL,      Load_OPTS, NULL, CH_RIFF},
01873   { 'PATS', Save_PATS, Load_PATS, NULL, CH_RIFF | CH_LAST},
01874 };
01875 
01876 static bool IsSignedVarMemType(VarType vt)
01877 {
01878   switch (GetVarMemType(vt)) {
01879     case SLE_VAR_I8:
01880     case SLE_VAR_I16:
01881     case SLE_VAR_I32:
01882     case SLE_VAR_I64:
01883       return true;
01884   }
01885   return false;
01886 }

Generated on Wed Dec 30 20:40:06 2009 for OpenTTD by  doxygen 1.5.6